我正在尝试制作一个从电话中加载联系人列表的应用程序,但这需要大量时间,具体取决于联系人的数量。我想在后台运行加载联系人列表,以免降低应用程序的速度。
我正在使用以下功能加载联系信息。

void loadContacts() {
    ContentResolver contentResolver=getContentResolver();
    Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
    if(cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Bitmap photo = retrieveContactPhoto(id);
            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
            String phoneNumber = null;
            if (hasPhoneNumber > 0) {
                Cursor cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                while (cursor2.moveToNext()) {
                    String ph = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    phoneNumber = ph.replaceAll("\\s", "");
                    System.out.println(phoneNumber);
                }
                cursor2.close();
            }
            if(phoneNumber==null)
                continue;
        }
    }
    cursor.close();
}

最佳答案

您需要使用加载程序来解决您的查询。使用装载机会给您带来一些好处。其中之一是:如果用户旋转手机并重新启动Activity,则将保留加载程序(不再需要查询数据库,因为对光标的引用在Loader中仍然可用),即,加载程序处于生命周期意识到的。您需要实现

 LoaderManager.LoaderCallbacks<Cursor>

像这样
 MyActivity extends AppCompatActivity implements
     LoaderManager.LoaderCallbacks<Cursor>{

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
                     //return a new instance of CursorLoader

                          return new CursorLoader(this) {
                                   //this reference will be preserved when activity rotates
                                   Cursor myCursor;


                                    @Override
                                   protected void onStartLoading() {
                                        super.onStartLoading();

                                        //myCursor will be null when activity is starting for the first time
                                        if (myCursor== null)
                                                 forceLoad();
                                         else
                                          deliverResult(myCursor);
        }

                                   @Override
                                   public Cursor loadInBackground() {
                                       //make your database query here

                                       myCursor = getContext().getContentResolver().query()
                                         return myCursor;

                                        }

                                 }


                      }

        @Override
      public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

                       //bind your Cursor to your activity view

                       }


         @Override
         public void onLoaderReset(Loader<Cursor> loader) {

                       }


          }

有关更多信息,请查看我的Udacity纳米级代码here第272-338行对其进行了更好的说明。

关于android - 如何在Android的后台运行代码块?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45159764/

10-09 03:09