Android剪切板的使用:android
String base64String = "我要複製的內容,放入剪切板中了"; //從Android系統中調用剪切板的服務 ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //在剪切板中放入要傳遞的數據 clipboardManager.setText(base64String);
import android.content.ClipData; import android.content.ClipDescription; import android.content.ClipboardManager; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import com.ly.sxh.data.LoaderApp; /** * Created by SRain on 2016/3/4. * <p/> * 讀取剪切板中內容 */ public class ClipboardUtils { public static final String MIME_TYPE_CONTACT = "vnd.android.cursor.item/vnd.xiaoma.contact"; public static String getClipboard(Context context) { String result = ""; LoaderApp app = (LoaderApp) context.getApplicationContext(); ClipboardManager clipboard = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE); ClipData.Item item = null; //無數據時直接返回 if (!clipboard.hasPrimaryClip()) { app.shortToast("剪貼板中無數據"); return result; } //若是是文本信息 if (clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { ClipData cdText = clipboard.getPrimaryClip(); item = cdText.getItemAt(0); //此處是TEXT文本信息 if (item.getText() == null) { return result; } else { String str = item.getText().toString(); if (str.contains("#樂遊口令#")) { result = analysisVerify(context, str); } } } else if (clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) { //此處是INTENT item = clipboard.getPrimaryClip().getItemAt(0); Intent intent = item.getIntent(); context.startActivity(intent); } else if (clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_URILIST)) { //此處是URI內容 ContentResolver cr = context.getContentResolver(); ClipData cdUri = clipboard.getPrimaryClip(); item = cdUri.getItemAt(0); Uri uri = item.getUri(); if (uri != null) { String mimeType = cr.getType(uri); if (mimeType != null) { if (mimeType.equals(MIME_TYPE_CONTACT)) { Cursor pasteCursor = cr.query(uri, null, null, null, null); if (pasteCursor != null) { if (pasteCursor.moveToFirst()) { //此處對數據進行操做就能夠了,前提是有權限 } } pasteCursor.close(); } } } } return result; } /** * 清除剪切板中的內容,暫時用空字符代替 * TODO * @param context */ public static void clearClipboard(Context context) { ClipboardManager clipboard = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE); ClipData.Item item = null; //無數據時直接返回 if (!clipboard.hasPrimaryClip()) { return; } //若是是文本信息 if (clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { String base64String = ""; //從Android系統中調用剪切板的服務 ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); //在剪切板中放入要傳遞的數據 clipboardManager.setText(base64String); } } private static String analysisVerify(Context context, String message) { String[] strs = message.split("進羣口令"); LoaderApp app = (LoaderApp) context.getApplicationContext(); char[] msgs = strs[strs.length - 1].toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < msgs.length; i++) { if ('0' <= msgs[i] && msgs[i] <= '9') { sb.append(msgs[i]); } } return sb + ""; } }