如何在异步任务中获取 phone 个联系人?

how to get phone contacts in async task?

我试图联系我发现需要一些时间等待。因此,我尝试在异步任务中将联系人写入应用程序数据库。这是我失败的尝试:

public class GetUpdateContactsTask extends AsyncTask<Void, Contact, Void> {

private static final String TAG = "lifeCycle";
private Context mContext;

public GetUpdateContactsTask (Context context){
    mContext = context;
}
public interface OnFinishListener{
    void onFinish();
}

public OnFinishListener finishListener;

public void setFinishListener(OnFinishListener finishListener){
    this.finishListener = finishListener;
}

@Override
protected void onPreExecute() {
}


@Override
protected Void doInBackground(Void... params) {
    ContentResolver cr = mContext.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) { // here is an NullPointerException error
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Contact contact = new Contact();
                    contact.setName(name);
                    contact.setPhoneNumber(phoneNo);
                    publishProgress(contact);
                }
                pCur.close();
            }
        }
    }
    return null;
}

@Override
protected void onProgressUpdate(Contact... values) {
    Contact contact = values[0];
    String name = contact.getName();
    String phoneNumber = contact.getPhoneNumber();
    if (!phoneNumber.equals("not valid")){
        Log.e(TAG, name + ": " + phoneNumber);
        if(!DataHelper.getInstance().isContactIsset(values[0])){
            DataHelper.getInstance().saveContact(values[0]);
            Log.e(TAG, "contact saved");
        } else {
            Log.e(TAG, "contact is isset");
        }
    }
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    finishListener.onFinish();
}

}

没用。游标上存在 nullpointerexception 错误。如果您能帮助更正我的版本就好了,但如果您能展示更好的解决方案就更好了。谢谢

您正在从 doInBackground 方法返回 null 并在 onProgressUpdate 中得到结果,它用于更新进度状态,而不是

return contact;

然后在onPostExecute方法中得到结果,如下

@Override
        protected void onPostExecute(Contact values) {
            super.onPostExecute(values);

          Contact contact = values[0];
          String name = contact.getName();
          ...............
          ...............

        }

原发帖者solution问题发帖错误

The problem was in Lenovo Security app. It blocks access to contacts. So I add my application to white list and problem was solved!