无法从htc(4.3.1)android

时间:2016-09-28 11:16:52

标签: android

我无法为以下问题找到任何合适的解决方案。我有一些方法可以从手机返回联系人姓名和电话号码,它适用于很多设备。

但是,问题这个方法对我的htc(android 4.3.1)不起作用。我认为,可能主要原因是在游标声明的行上(Cursor phones = getContentResolver()。query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);),因为我通过getCount方法检查了Cursor对象,它显示零,这意味着它无法获取任何数据。我尝试了许多声明Cursor对象的方法,但我没有成功。

提前谢谢!请检查下面提到的代码:

private List<String> getContactNames() {
        List<String> contacts = new ArrayList<>();
        // Get the ContentResolver
        ContentResolver cr = getContentResolver();
        // Get the Cursor of all the contacts
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (phones.moveToNext()) {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneNumber = phoneNumber.replaceAll("[()+]", "");
            phoneNumber = phoneNumber.replaceAll(" ", "");
            if (phoneNumber.startsWith("8")) {
                phoneNumber = phoneNumber.replaceFirst("8", "7");
            }
            //contactNames.add(phoneNumber);
            contacts.add(name + "&&" + phoneNumber);
        }
        phones.close();
        Collections.sort(contacts, String.CASE_INSENSITIVE_ORDER);
        List<String> contactList =
                new ArrayList<>(new LinkedHashSet<String>(contacts));
        for (int i = 0; i < contactList.size(); i++) {
            String tem = contactList.get(i);
            String[] arr = tem.split("&&");
            contactNumbers.add(arr[1]);
            contactNames.add(arr[0]);
        }

        //Toast.makeText(this, contactNames.toString(), Toast.LENGTH_LONG).show();
        return contactList;
    }

4 个答案:

答案 0 :(得分:0)

使用此功能。

public class ContactsProvider {

private Uri QUERY_URI = ContactsContract.Contacts.CONTENT_URI;
private String CONTACT_ID = ContactsContract.Contacts._ID;
private String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
private Uri EMAIL_CONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
private String EMAIL_CONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
private String EMAIL_DATA = ContactsContract.CommonDataKinds.Email.DATA;
private String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
private String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
private Uri PHONE_CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
private String PHONE_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
private String STARRED_CONTACT = ContactsContract.Contacts.STARRED;
private ContentResolver contentResolver;

public ContactsProvider() {
    contentResolver = GenericApplication.getContext().getContentResolver();
}

public List<Contact> getContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    String[] projection = new String[]{CONTACT_ID, DISPLAY_NAME, HAS_PHONE_NUMBER, STARRED_CONTACT};
    String selection = null;
    Cursor cursor = contentResolver.query(QUERY_URI, projection, selection, null, null);

    while (cursor.moveToNext()) {
        Contact contact = getContact(cursor);
        contactList.add(contact);
    }

    cursor.close();
    return contactList;
}

private Contact getContact(Cursor cursor) {
    String contactId = cursor.getString(cursor.getColumnIndex(CONTACT_ID));
    String name = (cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)));
    Uri uri = Uri.withAppendedPath(QUERY_URI, String.valueOf(contactId));

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    String intentUriString = intent.toUri(0);

    Contact contact = new Contact();
    contact.id = Integer.valueOf(contactId);
    contact.name = name;
    contact.uriString = intentUriString;

    getPhone(cursor, contactId, contact);
    getEmail(contactId, contact);
    return contact;
}

private void getEmail(String contactId, Contact contact) {
    Cursor emailCursor = contentResolver.query(EMAIL_CONTENT_URI, null, EMAIL_CONTACT_ID + " = ?", new String[]{contactId}, null);
    while (emailCursor.moveToNext()) {
        String email = emailCursor.getString(emailCursor.getColumnIndex(EMAIL_DATA));
        if (!TextUtils.isEmpty(email)) {
            contact.email = email;
        }
    }
    emailCursor.close();
}

private void getPhone(Cursor cursor, String contactId, Contact contact) {
    int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
    if (hasPhoneNumber > 0) {
        Cursor phoneCursor = contentResolver.query(PHONE_CONTENT_URI, null, PHONE_CONTACT_ID + " = ?", new String[]{contactId}, null);
        while (phoneCursor.moveToNext()) {
            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(PHONE_NUMBER));
            contact.phone = phoneNumber;
        }
        phoneCursor.close();
    }
}

}

答案 1 :(得分:0)

使用此方法:

    private void LoadContactsWithPhone() {
    TextView contactView = (TextView) getView().findViewById(R.id.txtContacts);
    contactView.setText("");

    Cursor cursor = getContactsWithPhone();

    while (cursor.moveToNext()) {

        String displayName = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));

        String phoneNo = cursor.getString(cursor
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        contactView.append("Name: ");
        contactView.append(displayName);
        contactView.append("\n");

        contactView.append("Phone: ");
        contactView.append(phoneNo);
        contactView.append("\n\n");

    }

}
    private Cursor getContactsWithPhone() {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[]{ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };

    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";

    String[] selectionArgs = new String[]{String.valueOf(1)};

    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return getActivity().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
}

答案 2 :(得分:0)

请使用以下代码在所有设备上使用以下代码: -

  public ArrayList<Contact_Model> getContacts(){

    ArrayList<Contact_Model> contact_models = new ArrayList<>();
    //        ContentResolver cr = context.getActivity().getContentResolver();
            ContentProviderClient mCProviderClient = context.getActivity().getContentResolver().acquireContentProviderClient(ContactsContract.Contacts.CONTENT_URI);
            Cursor cur = mCProviderClient.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    Contact_Model contact_model = new Contact_Model();
                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    String firstname;
                    String lastName = "";
                    if (name != null) {

                        contact_model.setContactId(id);
                        contact_model.setContactName(name);
                        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            // get the phone number
                            Cursor pCur = mCProviderClient.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                    new String[]{id}, null);

                            while (pCur.moveToNext()) {
                                String phone = pCur.getString(
                                        pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                contact_model.setContactNumber(phone);
                            }
                            pCur.close();


                            // get email and type

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

                            while (emailCur.moveToNext()) {
                                // This would allow you get several email addresses
                                // if the email addresses were stored in an array
                                String email = emailCur.getString(
                                        emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                                contact_model.setContactEmail(email);
                            }
                            emailCur.close();

                            Cursor photoCursor = mCProviderClient.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                    new String[]{id}, null);
                            while (photoCursor.moveToNext()) {
                                String photo = photoCursor.getString(photoCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
                                if (photo == null) {
                                    contact_model.setContactPhoto("");
                                } else {
                                    contact_model.setContactPhoto(photo);
                                }

                            }
                            contact_model.setContactOtherDetails("");
                            photoCursor.close();

                            // Get note.......
                            String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                            String[] noteWhereParams = new String[]{id,
                                    ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
                            Cursor noteCur = mCProviderClient.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
                            if (noteCur.moveToFirst()) {
                                String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                                System.out.println("Note " + note);
                                contact_model.setNote(note);
                            }

                            noteCur.close();

                            // Get Organizations.........

                            String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                            String[] orgWhereParams = new String[]{id,
                                    ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
                            Cursor orgCur = mCProviderClient.query(ContactsContract.Data.CONTENT_URI,
                                    null, orgWhere, orgWhereParams, null);
                            if (orgCur.moveToFirst()) {
                                String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
                                String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
                                if (orgName != null) {
                                    contact_model.setOrganization(orgName);
                                }
                                if (title != null) {
                                    contact_model.setJob(title);
                                }
                            }
                            orgCur.close();

                        }

                        contact_models.add(contact_model);
                    }
                }
            }
            return contact_models;

    }



Contact Model:-

public class Contact_Model implements Serializable {

    private String contactId = "", contactName = "", contactNumber = "", contactEmail = "", contactPhoto = "", contactOtherDetails = "";

    private String firstName = "", lastName = "", note = "", organization = "", job = "", dob = "";

    ContactAccount contactAccount;

    public Contact_Model() {

    }



    public Contact_Model(String contactId, String contactName, String contactNumber, String contactEmail, String contactPhoto, String contactOtherDetails, String firstName, String lastName,
                         String note, String organization, String job, String dob) {
        this.contactId = contactId;
        this.contactName = contactName;
        this.contactNumber = contactNumber;
        this.contactEmail = contactEmail;
        this.contactPhoto = contactPhoto;
        this.contactOtherDetails = contactOtherDetails;
        this.firstName = firstName;
        this.lastName = lastName;
        this.note = note;
        this.organization = organization;
        this.dob = dob;
        this.job = job;
    }

    public ContactAccount getContactAccount() {
        return contactAccount;
    }

    public void setContactAccount(ContactAccount contactAccount) {
        this.contactAccount = contactAccount;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getContactId() {
        return contactId;
    }

    public void setContactId(String contactId) {
        this.contactId = contactId;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getContactEmail() {
        return contactEmail;
    }

    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }

    public String getContactPhoto() {
        return contactPhoto;
    }

    public void setContactPhoto(String contactPhoto) {
        this.contactPhoto = contactPhoto;
    }

    public String getContactOtherDetails() {
        return contactOtherDetails;
    }

    public void setContactOtherDetails(String contactOtherDetails) {
        this.contactOtherDetails = contactOtherDetails;
    }
}


ContactAccount.class

public class ContactAccount implements Serializable {

    String accountName = "", accountType = "";

    public ContactAccount(){

    }

    public ContactAccount(String accountName, String accountType) {
        this.accountName = accountName;
        this.accountType = accountType;
    }


    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getAccountType() {
        return accountType;
    }

    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }
}

希望这可以帮助你..

答案 3 :(得分:0)

对不起家伙,主要原因是设备本身,我检查了使用设备联系人作为whatsapp的其他类似应用程序,这些应用程序也无法使用手机的联系人。这里提出的所有答案都完美无缺,与我的方法一样好。