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

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void copyToClipboard(Context context, String text) {
    // if()/*from   w  w  w .j a  v  a2  s.c om*/
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
    }
}

From source file:Main.java

/**
 * copy plain text to clip board/* w ww  . java  2 s.com*/
 *
 * @param context
 * @param tag
 * @param text
 * @param toastText may null, no toast at all
 */
public static void copy2ClipBoard(Context context, String tag, String text, String toastText) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(tag, text);
    clipboard.setPrimaryClip(clip);
    if (toastText != null) {
        Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show();
    }
}

From source file:Main.java

private static ClipboardManager getClipboard(Context context) {
    return (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
}

From source file:Main.java

/**
 * <pre>/*from   www  .  java2s . c o m*/
 * Copy text to clipboard.
 * Different implementation for API < 11 and API >= 11.
 * </pre>
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void copyTextToClipboard(String text) {
    if (Build.VERSION.SDK_INT < 11) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getCurrentContext()
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getCurrentContext()
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText("PoketChat Copy To Clipboard", text));
    }
}

From source file:Main.java

/**
 * ClipboardUitls to set a plain text into system clipboard
 * //from   w  w  w  .j av  a2  s .  c o  m
 * @param context  the application context
 * @param label    the User-visible label for the clip data (for android version HONEYCOMB or higher only)
 * @param text     The actual text in the clip.
 */
public static void setPlainText(Context context, String label, String text) {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    } else {
        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 source file:Main.java

/**
 * Return true if there is text in the clipboard
 * @param ctx/*  www  .j a va2  s .  c  o  m*/
 * @return
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static boolean checkForText(Context ctx) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (clipboard == null) {
            clipboard = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
        }
        return clipboard.hasPrimaryClip();
    } else {
        if (oldClipboard == null) {
            oldClipboard = (android.text.ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
        }
        return oldClipboard.hasText();
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static void copyToClipboard11AndLater(@NonNull Context context, @NonNull String toCopy) {
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", toCopy);
    clipboard.setPrimaryClip(clip);//from   w  ww. ja va 2  s .  c om
}

From source file:Main.java

public static boolean copyStringToClipboard(Context context, String label, String src) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(label, src);
    clipboard.setPrimaryClip(clip);/*from ww  w. j a v  a  2  s .c  o m*/
    return true;
}

From source file:Main.java

public static void copyToClipBoard(Context context, String content) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    cm.setText(content);//from  w w w.  ja va 2s.  c o m
}

From source file:Main.java

/**
 * Copy tags to clipboard as multi-line text in the form
 * key1=value1/*from  www.  j  ava 2  s .  com*/
 * key2=value2
 * .....
 * @param tags
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void copyTags(Context ctx, Map<String, String> tags) {

    StringBuffer tagsAsText = new StringBuffer();

    for (String key : tags.keySet()) {
        tagsAsText.append(key + "=" + tags.get(key) + "\n");
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipboardManager clipboard = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("OSM Tags", tagsAsText.toString());
        clipboard.setPrimaryClip(clip);
    } else {
        android.text.ClipboardManager oldClipboard = (android.text.ClipboardManager) ctx
                .getSystemService(Context.CLIPBOARD_SERVICE);
        oldClipboard.setText(tagsAsText.toString());
    }
}