获取联系人的光标返回null

时间:2016-03-14 07:25:03

标签: android android-contacts

在我的应用程序中,我想从电话簿中获取所有联系人。我写的代码总是给我空cursor

代码

//在后台获取联系人

static class GetContacts extends AsyncTask<Void, Void, List<ContactsModel>> {
    private Context context;
    private ContentResolver contactResolver;
    private ContactsModel contactsModel;
    private List<String> listPhones = Collections.emptyList();
    private List<String> listEmails = Collections.emptyList();
    private List<ContactsModel> listContacts = Collections.emptyList();
    private String phone;
    private String email;
    private String id;
    private Cursor phoneCursor;
    private Cursor emailCursor;

    public GetContacts(Context context) {
        this.context = context;

    }

    @Override
    protected List<ContactsModel> doInBackground(Void... params) {

        contactResolver = context.getContentResolver();
        Cursor cur = contactResolver.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                contactsModel = new ContactsModel();

                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                contactsModel.name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                    phoneCursor = contactResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id}, null);

                    while (phoneCursor.moveToNext()) {
                        // Do something with phones
                        int phoneType = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                        String phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        //you will get all phone numbers according to it's type as below switch case.
                        switch (phoneType) {
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                phone = phoneNo;
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                phone = phoneNo;
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                phone = phoneNo;
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
                                phone = phoneNo;
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                                phone = phoneNo;
                                break;
                            default:
                                break;
                        }

                        listPhones.add(phone);
                    }


                    emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);

                    while (emailCursor.moveToNext()) {
                        //String emai = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                        String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");
                        listEmails.add(emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));

                    }

                    listContacts.add(contactsModel);
                    emailCursor.close();
                    phoneCursor.close();


                }


            }


        }

        cur.close();


        return listContacts;
    }

    @Override
    protected void onPostExecute(List<ContactsModel> contactsModels) {


    }
}

//活动代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_contact_list);
        init();

    }


    protected void init() {
        context = this;
        progressBar = (ProgressBar) findViewById(R.id.pb_all_contacts);
        new GetContacts(context).execute();


    }

我的地址簿中有超过1000个联系人

P.S - I also have requested permission to read contact in manifest

2 个答案:

答案 0 :(得分:0)

在您的代码中,我看到您的onPostExecute方法为空。我认为在此方法中您必须设置已填充的list

@Override
    protected void onPostExecute(List<ContactsModel> contactsModels) {
       listContacts = contactsModels;
    }

因为你在doInBackground中通过手机检索数据但是你通过了它?希望能帮到你!

答案 1 :(得分:0)

您需要在查询内容提供商时进行投影。

String[] mProjection =
{
    UserDictionary.Words._ID,    // Contract class constant for the _ID column name
    UserDictionary.Words.WORD,   // Contract class constant for the word column name
    UserDictionary.Words.LOCALE  // Contract class constant for the locale column name
};


contactResolver = context.getContentResolver();
        Cursor cur = contactResolver.query(ContactsContract.Contacts.CONTENT_URI,
                mProjection, null, null, null);
上面的

只是查询内容提供者的投影示例。

你可以参考这个链接, http://developer.android.com/guide/topics/providers/content-provider-basics.html http://developer.android.com/training/contacts-provider/retrieve-details.html