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

public static String getClipData(Context context) {
    ClipboardManager clip = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    return clip.getPrimaryClip().getItemAt(0).getText().toString();
}

From source file:Main.java

private static void copyToClipboard10AndBefore(@NonNull Context context, @NonNull String toCopy) {
    @SuppressWarnings("deprecation")
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(toCopy);/*  w w  w .  j  a  va 2 s . co  m*/
}

From source file:Main.java

/**
 * This is only called when oldAPi is passed as true on {@link #addItemToClipboard(Context, String, String, boolean)}
 * @param context The context, required to get the Cliboard System Service. 
 * @param value The value to store on the clipboard. 
 *///from w  w w. ja  va 2  s .c  om
@SuppressWarnings("deprecation")
private static void addTextToClipboard(final Context context, final String value) {
    android.text.ClipboardManager manager = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    manager.setText(value);
}

From source file:Main.java

/**
 * Copy uri to Clipboard//from   ww w.  java2s . c  om
 *
 * @param context
 * @param uri
 */
public static void copy(Context context, Uri uri) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setPrimaryClip(ClipData.newUri(context.getContentResolver(), null, uri));
}

From source file:Main.java

public static void setClipBoard(final Context context, final String msg, Handler handler) {

    Runnable updater = new Runnable() {
        public void run() {

            ClipboardManager clip = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData data = ClipData.newPlainText("Remoteroid_Clip", msg);
            clip.setPrimaryClip(data);/*from w  w w.  ja va 2  s.c o  m*/

        }
    };
    handler.post(updater);

}

From source file:Main.java

public static void addToClipboard(Context ctx, String text) {
    ClipboardManager clipboardManager = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.setPrimaryClip(ClipData.newPlainText(CLIPBOARD_LABEL, text));
}

From source file:Main.java

@SuppressLint("NewApi")
public static void copyToClipboard(Context context, String message) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clip = ClipData.newPlainText("", message);
        clipboard.setPrimaryClip(clip);/* w w w.  j  a v a2 s  .  c  o m*/
    } else {
        clipboard.setText(message);
    }
}

From source file:Main.java

/**
 * This will copy text to the device's clipboard.
 *
 * @param context an application or activity context
 * @param text the string to be copied to the clipboard
 * @param description a user-visible label for the copied text
 *///from  w  w  w.  ja va 2 s. c o  m
@TargetApi(VERSION_CODES.HONEYCOMB)
public static void copyText(Context context, String text, String description) {
    if (IS_AT_LEAST_HC) {
        ClipData clip = ClipData.newPlainText(description, text);
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(clip);
    } else {
        android.text.ClipboardManager cm = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(text);
    }
}

From source file:Main.java

/**
 * This is only called when we are on API level > Honeycomb
 * @param context The context, required to get the Cliboard System Service. 
 * @return Returns the latest item added to the clipboard by the user, as long as the {@code MimeType} is {@code MIMETYPE_TEXT_PLAIN}
 *///w  w w .  j  av  a 2s .c  o  m
@SuppressLint("NewApi")
public static String getItemFromCliboard(final Context context) {
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    final ClipData data = manager.getPrimaryClip();

    if (data == null) { //If no data on clipboard, let's call it a day. 
        return EMPTY_STRING;
    }

    final String clipItem;

    if (data.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        ClipData.Item item = data.getItemAt(0);
        clipItem = item.getText().toString();
    }

    else {
        clipItem = EMPTY_STRING;
    }

    return clipItem;

}

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 {//ww  w . j  a  v a  2 s .  c  o  m
        return clipboardManager.hasText();
    }
}