Try Get Text From Clipboard - Android Android OS

Android examples for Android OS:Clipboard

Description

Try Get Text From Clipboard

Demo Code


import android.content.Context;
import android.text.ClipboardManager;

public class Main{
    public static boolean TryGetTextFromClipboard(Context context,
            Holder<String> text) {
        try {//  ww  w .  j a  v  a 2  s  .c om
            ClipboardManager clipboard = (ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            if (clipboard != null) {
                CharSequence clipboardText = clipboard.getText();
                if (clipboardText != null && clipboardText.length() > 0) {
                    String textResult = clipboardText.toString();
                    if (!textResult.startsWith("intent")) {
                        text.value = textResult;
                        return true;
                    }
                }
            }
        } catch (Exception e) {

        }
        return false;
    }
}

Related Tutorials