代码:
public static String[] getClipboard(Context context) {
String[] result = null;
try {
Looper.prepare();
ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
CharSequence text = cm.getText();
if (text != null && text.toString().length() > 0) {
result = new String[] { text.toString() };
}
// Looper.loop();
} catch (Exception e) {
Log.d(TAG, "getClipboard(): " + e);
}
return result;
}
private class UpdateTask extends AsyncTask<Void, HistoryItem, Void> {
private ProgressDialog dialog;
protected Void doInBackground(Void... params) {
try {
HistoryItem item = new HistoryItem(HistoryEnum.CLIPBOARD);
item.records = HistoryUtil.getClipboard(getContext());
...
logcat的:
getClipboard(): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()