Example usage for android.content Context CLIPBOARD_SERVICE

List of usage examples for android.content Context CLIPBOARD_SERVICE

Introduction

In this page you can find the example usage for android.content Context CLIPBOARD_SERVICE.

Prototype

String CLIPBOARD_SERVICE

To view the source code for android.content Context CLIPBOARD_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.content.ClipboardManager for accessing and modifying the contents of the global clipboard.

Usage

From source file:Main.java

/**
 * Get intent from Clipboard/*  ww  w  . j av  a  2 s .  c om*/
 *
 * @param context
 * @return
 */
public static Intent getIntent(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()
            && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
            return clip.getItemAt(0).getIntent();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Copy text to Clipboard//w w  w .  j  a v a  2  s .c  om
 *
 * @param context
 * @param text
 */
public static void copy(Context context, String text) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
}

From source file:Main.java

@SuppressLint("NewApi")
public static void copyToClipboard(Context context, String label, String text) {
    if (isHoneycombOrHigher()) {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText(label, text);
        clipboard.setPrimaryClip(clip);/*from w ww . ja v  a 2s. c om*/
    } else {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    }
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void clearClipboard(Context context) {
    android.text.ClipboardManager manager = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    manager.setText(EMPTY_STRING);//from www .  j av  a 2 s.c om
}

From source file:Main.java

public static void copyToClipBoard(Context context, String text, String success) {
    ClipData clipData = ClipData.newPlainText("meizhi_copy", text);
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    manager.setPrimaryClip(clipData);/*from  w ww.  j a v  a  2s .  c  o m*/
    Toast.makeText(context, success, Toast.LENGTH_SHORT).show();
}

From source file:Main.java

public static CharSequence getText(final Context context) {
    if (context == null)
        return null;
    return ((ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE)).getText();
}

From source file:Main.java

/**
 * Copy a text to the clipboard./*from ww w .j  a v  a2s  .  c o  m*/
 * @param context The current context.
 * @param text The text to copy.
 * @param toastMessage The message to show in a Toast notification. If empty or null, does not display notification.
 */
public static void copyTextToClipboard(Context context, String text, String toastMessage) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setPrimaryClip(ClipData.newPlainText(text, text));

    if ((toastMessage != null) && (toastMessage.length() > 0)) {
        Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
    }
}

From source file:Main.java

public static boolean setText(final Context context, final CharSequence text) {
    if (context == null)
        return false;
    ((ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE)).setText(text != null ? text : "");
    return true;/*from   www  . ja  v a 2  s .com*/
}

From source file:Main.java

public static boolean setText(final Context context, final CharSequence text) {
    if (context == null)
        return false;
    final ClipboardManager clipboardManager = (ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.setPrimaryClip(ClipData.newPlainText(text, text));
    return true;//w  w w . j  a  v a  2 s  .co  m
}

From source file:Main.java

/**
 * Copy a text to the clipboard./*from w w  w  . j  av a  2 s  .c om*/
 * 
 * @param context
 *            The current context.
 * @param text
 *            The text to copy.
 * @param toastMessage
 *            The message to show in a Toast notification. If empty or null,
 *            does not display notification.
 */
public static void copyTextToClipboard(Context context, String text, String toastMessage) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(text);

    if ((toastMessage != null) && (toastMessage.length() > 0)) {
        Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
    }
}