Example usage for android.content ClipboardManager hasText

List of usage examples for android.content ClipboardManager hasText

Introduction

In this page you can find the example usage for android.content ClipboardManager hasText.

Prototype

@Deprecated
public boolean hasText() 

Source Link

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
public static boolean hasText(Context context) {
    ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clipData = clipboardManager.getPrimaryClip();
        return hasText(clipboardManager, clipData);
    } else {//  w  w w. j a v a 2 s .co m
        return clipboardManager.hasText();
    }
}

From source file:Main.java

public static boolean hasText(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        return clipData != null && description != null
                && (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN));
    } else {//from  www .j  ava2  s .c om
        //noinspection deprecation
        return cm.hasText();
    }
}

From source file:de.syss.MifareClassicTool.Common.java

/**
 * Get the content of the Android clipboard (if it is plain text).
 * @param context Context of the SystemService
 * @return The content of the Android clipboard. On error
 * (clipboard empty, clipboard content not plain text, etc.) null will
 * be returned./*from www  .  j a v a 2s. com*/
 */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static String getFromClipboard(Context context) {
    if (Build.VERSION.SDK_INT >= 11) {
        // Android API level 11+.
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.getPrimaryClip() != null && clipboard.getPrimaryClip().getItemCount() > 0 && clipboard
                .getPrimaryClipDescription().hasMimeType(android.content.ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            return clipboard.getPrimaryClip().getItemAt(0).getText().toString();
        }
    } else {
        // Android API level 10.
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.hasText()) {
            return clipboard.getText().toString();
        }
    }

    // Error.
    return null;
}