如何在线程内使用Cursor进行查询? (机器人)

时间:2015-06-19 09:46:00

标签: android android-cursor android-cursorloader

我最近开始上大学的第一个应用程序,作为我的应用程序的一部分,我想使用this guide.

访问手机的联系人

在指南中,onActivityResult看起来像这样:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {Phone.NUMBER};

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            // Do something with the phone number...
        }
    }
}

它说我应该使用一个线程或CursorLoader来进行查询,但到目前为止我无法为此找到一个好的解决方案。如果我将查询方法放在一个线程中,那么我就无法从中访问数据:

    Runnable r = new Runnable() {
                    @Override
                    public void run() {
                        Cursor cursor = getContentResolver()
                                .query(contactUri, projection, null, null, null);
                    }
                };
                Thread queryThread = new Thread(r);
                queryThread.start();
cursor.moveToFirst();
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

所以在这段代码中Android Studio无法解析符号“cursor”:( 到目前为止,我找不到我理解如何使用CursorLoaders进行此操作的指南。

2 个答案:

答案 0 :(得分:3)

您可以使用AsyncTask作业!

     class WorkCursor extends AsyncTask<Cursor,Object,String> {

            String[] projection;
            Uri contactUri;

            public WorkCursor(String[] projection,Uri contactUri){
                this.contactUri = contactUri;
                this.projection = projection;
            }


            @Override
            protected String doInBackground(Cursor... cursors) {

                //This is done in the background

                Cursor cursor = MyActivity.this.getContentResolver()
                        .query(contactUri, projection, null, null, null);

                int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String number = cursor.getString(column);

                return number;
            }

            @Override
            protected void onPostExecute(String number) {
                super.onPostExecute(number);

                //This is done on the UI thread
                functionCall(number);

            }
        }

        public void functionCall(String number){
            //This is the UI thread
            //You can do whatever you with your number
            Toast.makeText(this,"This is the number: "+number,Toast.LENGTH_SHORT).show();
        }

然后像这样调用Asynctask:

new WorkCursor(projection,contactUri).execute();

另一种方法是像你一样做但是在线程中完成所有工作然后在UI线程上运行结果,如下所示:

new Thread(new Runnable() {
            @Override
            public void run() {
                Cursor cursor = getContentResolver()
                        .query(contactUri, projection, null, null, null);

                cursor.moveToFirst();
                int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                final String number = cursor.getString(column);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        functionCall(number);
                    }
                });
            }
        }).run();

字符串编号必须是最终的才能在另一个(在UI上运行)线程中访问!

免责声明:代码未经测试。

答案 1 :(得分:0)

您似乎无法理解启动单独线程意味着什么。光标不仅在示例中超出范围,而且还异步初始化,这意味着在您调用start()后它不会立即可用。 start()总是立即完成,只是导致线程在一段时间后执行,而不会阻塞主线程。

在android中,对于大多数用例来说,使用AsyncTask最方便,而不是手动创建单独的线程。有关详细信息,请参阅Processes and Threads guide