如何从单个联系人中分离多个电话

时间:2017-09-06 21:01:58

标签: android

如果手机有多个号码,如何将手机与联系人分开,如图所示

MG8ou.png

这里是我的代码

    @Override
    protected Void doInBackground(Void... params) {
        ContentResolver contentResolver = getActivity().getContentResolver();

        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));

                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    ContactObjets contactObjets = new ContactObjets();

                    contactObjets.setContactName(name);

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

                    if (phoneCursor.moveToNext()) {

                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        contactObjets.setContactNumber(phoneNumber);
                    }
                    phoneCursor.close();

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


                    while (emailCursor.moveToNext()) {
                        String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        contactObjets.setContactEmail(emailId);
                    }
                    emailCursor.close();

                    contactObjetsList.add(contactObjets);
                }
            }

        }
        return null;
    }

我把它放在AsyncTask中,就像你在对话框中看到的那样,它只调用第一个数字,但是如果它有多个数字,联系人就不会这样做。

我该如何解决我的问题

1 个答案:

答案 0 :(得分:0)

        @Override
    protected Void doInBackground(Void... params) {
        ContentResolver contentResolver = getActivity().getContentResolver();

        Cursor cursor = contentResolver.query(
                Contacts.CONTENT_URI,
                null,
                null,
                null,
                Phone.DISPLAY_NAME + " ASC");

            while (cursor.moveToNext()) {
                    String id = cursor.getString(cursor.getColumnIndex(Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));

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

                    if (phoneCursor.moveToNext()) {
                        for (int i = 0; phoneCursor.getCount() > i; i++ ){
                            phoneCursor.moveToPosition(i);
                            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));
                            int typePhone = phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE));
                            String type = "";
                            switch (typePhone){
                                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                    type = "Casa";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                    type = "Celular";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                    type = "Trabajo";
                                    break;
                                default:
                                    type = "Otro";
                                    break;
                            }
                            ContactObjets contactObjets = new ContactObjets();
                            contactObjets.setContactName(name);
                            contactObjets.setContactNumber(phoneNumber);
                            contactObjets.setContactType(type);
                            contactObjetsList.add(contactObjets);
                        }
                    }
                    phoneCursor.close();
            }
        return null;
    }