read From Clipboard primary clib - Android Android OS

Android examples for Android OS:Clipboard

Description

read From Clipboard primary clib

Demo Code


//package com.java2s;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

public class Main {
    public static String readFromClipboard(Context context) {
        // Gets the ClipboardManager
        ClipboardManager clipboard = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);

        // Gets the clipboard data from the clipboard
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null) {

            // Gets the first item from the clipboard data
            ClipData.Item item = clip.getItemAt(0);

            CharSequence text = item.getText();
            if (text != null && text.length() > 0) {
                return text.toString();
            }/*from www .  ja va2s. c  o m*/
            // return coerceToText(context, item).toString();
        }

        return null;
    }
}

Related Tutorials