阅读剪贴板时为什么需要Looper.prepare()?

时间:2013-09-18 01:56:08

标签: android clipboard history

代码:

    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()

0 个答案:

没有答案