Android的剪切板(ClipboardManager)
注意:导包的时候
API 11之前: android.text.ClipboardManager
API 11之后: android.content.ClipboardManager
/**
@param content要分享的字符串
**/
public void setClipBoard(String content) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", content);
clipboard.setPrimaryClip(clip);
} else {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(content);
}
}
/**
* 实现粘贴功能
*
* @param context
* @return
*/
public static String paste(Context context) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
android.content.ClipboardManager
clipboard =(android.content.ClipboardManager)
context.getSystemService(Context.CLIPBOARD_SERVICE);
return clipboard.getText().toString().trim();
} else {
android.text.ClipboardManager
clipboard = (android.text.ClipboardManager)
context.getSystemService(Context.CLIPBOARD_SERVICE);
return clipboard.getText().toString().trim();
}
}