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")
private static String getClipboardTextLegacy(Context ctx) {
    String txt = null;//from w ww. ja va  2  s  . co m
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) ctx
            .getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard != null) {
        txt = clipboard.getText().toString();
    }
    return txt;
}

From source file:Main.java

/**
 * //  www.j  a va 2 s . c o  m
 * @param context
 * @return
 */
public static CharSequence pasteToResult(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clipData = ((android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE)).getPrimaryClip();
        if ((clipData == null) || (clipData.getItemCount() <= 0)) {
            return null;
        }

        ClipData.Item item = clipData.getItemAt(0);
        if (item == null) {
            return null;
        }
        return item.getText();
    }

    return ((android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE)).getText();
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void setText(Context context, CharSequence sequence) {
    ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clip = ClipData.newPlainText("ClipboardManagerUtil", sequence);
        clipboardManager.setPrimaryClip(clip);
    } else {//ww  w  . j  ava  2 s  . c o  m
        clipboardManager.setText(sequence);
    }
}

From source file:Main.java

public static void copyToClipBoard(Context context, String text) {
    ClipData clipData = ClipData.newPlainText("text_copy", text);
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    manager.setPrimaryClip(clipData);/*  w  w  w. j a va  2  s . c om*/
}

From source file:Main.java

/**
 * //from   ww  w.  j  a  v a  2 s  .  co  m
 * @param context
 * @param label User-visible label for the clip data.
 * @param text The actual text in the clip.
 */
public static void copyFromEdit(Context context, CharSequence label, CharSequence text) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ((android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE))
                .setPrimaryClip(ClipData.newPlainText(label, text));
        return;
    }

    ((android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE)).setText(text);
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static String getText(Context context) {
    ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clipData = clipboardManager.getPrimaryClip();
        if (hasText(clipboardManager, clipData)) {
            return clipData.getItemAt(0).getText().toString();
        } else {//from www.  j  av a  2  s  .c  o  m
            return null;
        }
    } else {
        return clipboardManager.getText().toString();
    }
}

From source file:Main.java

/**
 * Get text from Clipboard//from  w w w. j  a  v  a 2  s  .c  o  m
 *
 * @param context
 * @return
 */
public static String getText(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()
            && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
            return String.valueOf(clip.getItemAt(0).coerceToText(context));
        }
    }
    return null;
}

From source file:Main.java

public static void clip(Context context, String text) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setText(text);/*from  w w w.ja  va2  s .  c om*/
    } else {
        ClipboardManager clipboardManager = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setPrimaryClip(ClipData.newPlainText("content", text));
    }
}

From source file:Main.java

/**
 * Get uri from Clipboard/*from   ww  w.j  a  v  a2 s .  c om*/
 *
 * @param context
 * @return
 */
public static Uri getUri(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()
            && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_URILIST)) {
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
            return clip.getItemAt(0).getUri();
        }
    }
    return null;
}

From source file:Main.java

public static void copyToClipboard(Context context, String label, String string, int toastMessageId) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(label, string);
    clipboard.setPrimaryClip(clip);//from   www.j a  v  a2  s .co m

    if (toastMessageId > 0) {
        Toast.makeText(context, toastMessageId, Toast.LENGTH_LONG).show();
    }
}