I tried to get contacts i saw that it need some time to wait. So, i tried to do it in async task with writting contacts to app db. Here is my failed attempt:
我试图找到联系人,我看到它需要一些时间等待。因此,我尝试在将联系人写入应用程序数据库的异步任务中执行此操作。这是我失败的尝试:
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();
}
}
it's not work. There is a nullpointerexception error on cursor. It will good if you help to correct my version, but it will great if you show your better solution. Thanks
这不行。游标上存在nullpointerexception错误。如果您帮助纠正我的版本会很好,但如果您展示更好的解决方案,它会很棒。谢谢
2 个解决方案
#1
1
Original posters solution incorrectly posted in question
原始海报解决方案错误地发布了问题
The problem was in Lenovo Security app. It blocks access to contacts. So I add my application to white list and problem was solved!
问题出在Lenovo Security应用程序中。它会阻止访问联系人。所以我将我的应用程序添加到白名单,问题解决了!
#2
0
You are returning null
from doInBackground
method and getting result in onProgressUpdate
, it is used for update progress status , instead
您从doInBackground方法返回null并将结果输入onProgressUpdate,它用于更新进度状态,而不是
return contact;
then get the result in onPostExecute
method , like below
然后在onPostExecutemethod中得到结果,如下所示
@Override
protected void onPostExecute(Contact values) {
super.onPostExecute(values);
Contact contact = values[0];
String name = contact.getName();
...............
...............
}
#1
1
Original posters solution incorrectly posted in question
原始海报解决方案错误地发布了问题
The problem was in Lenovo Security app. It blocks access to contacts. So I add my application to white list and problem was solved!
问题出在Lenovo Security应用程序中。它会阻止访问联系人。所以我将我的应用程序添加到白名单,问题解决了!
#2
0
You are returning null
from doInBackground
method and getting result in onProgressUpdate
, it is used for update progress status , instead
您从doInBackground方法返回null并将结果输入onProgressUpdate,它用于更新进度状态,而不是
return contact;
then get the result in onPostExecute
method , like below
然后在onPostExecutemethod中得到结果,如下所示
@Override
protected void onPostExecute(Contact values) {
super.onPostExecute(values);
Contact contact = values[0];
String name = contact.getName();
...............
...............
}