ContactsContract不返回电话号码,而是返回姓名

时间:2015-04-16 08:53:51

标签: android android-contacts contactscontract

我已经使用此代码显示我的手机的名称和电话号码,即使它只返回名称,我将不胜感激任何答案。如果我同时使用谷歌帐户,它是否也有效?

CODE: 公共类MainActivity扩展ActionBarActivity {

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

    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (people != null && people.moveToFirst()) {
        do {
            try {
                int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                String contact = people.getString(nameFieldColumnIndex);
                Log.d("CONTACT: ", contact);
                int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                String number = people.getString(numberFieldColumnIndex);
                Log.d("NUMBER: ", number);
            } catch (Exception e) {
                Log.e("ERROR: ", e.toString());
            }

        } while (people.moveToNext());
    }
}

ERROR:

  04-16 05:16:27.839    1426-1426/com.chatter.contactsaccess D/CONTACT:﹕ Test
04-16 05:16:27.839    1426-1426/com.chatter.contactsaccess D/NUMBER:﹕ 1

已编辑

3 个答案:

答案 0 :(得分:0)

没有

  

ContactsContract.PhoneLookup.NUMBER

出现在

  

ContactsContract.Contacts.CONTENT_URI

,使用

  

ContactsContract.Contacts.HAS_PHONE_NUMBER

http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html

Try this way,

String name = c=.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

答案 1 :(得分:0)

您可以尝试以下代码来获取手机中的所有手机通讯录。

在您的活动的onCreate中传递打开手机联系人的意图,如

private static final int PICK_CONTACT=1;

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                    startActivityForResult(intent, PICK_CONTACT);

在onAcivityResult中获取所选联系人的详细信息,如下所示

public void onActivityResult(int reqCode, int resultCode, Intent data) {
        if(reqCode==0)return;
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (PICK_CONTACT) :
                if (resultCode == Activity.RESULT_OK) {

                    Uri contactData = data.getData();
                    ContentResolver cr=getActivity().getContentResolver();
                    final Cursor c =  cr.query(contactData, null, null, null, null);

                    if (c.moveToFirst()) {
                        ArrayList<String> alNumbers=new ArrayList<>();


                        String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                        String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                        if (hasPhone.equalsIgnoreCase("1")) {
                            Cursor phones = getActivity().getContentResolver().query(
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
                                    null, null);
                            if(phones.moveToFirst())
                            {
                                while(!phones.isAfterLast())
                                {
                                    cNumber = phones.getString(phones.getColumnIndex("data1"));
                                    alNumbers.add(cNumber);
                                    phones.moveToNext();
                                }
                            }
                            phones.close();
                            final CharSequence[] items = alNumbers.toArray(new String[alNumbers.size()]);
                            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            builder.setTitle("Choose a number");
                            builder.setItems(items, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int item) {
                                    cNumber = items[item].toString();
                                    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                    phone.setText(name+" ("+cNumber+")");
                                }
                            });
                            AlertDialog alert = builder.create();
                            if(alNumbers.size() > 1) {
                                alert.show();



                               }
 name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}

答案 2 :(得分:0)

我通过更改ContactsContract.Contacts.CONTENT_URI

ContactsContract.CommonDataKinds.Phone.CONTENT_URI来修复它

剪切代码如下(我只想获取字符串中的名称和电话):

Cursor people = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        while(people.moveToNext()) {
            int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String contact = people.getString(nameFieldColumnIndex);
            int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number = people.getString(numberFieldColumnIndex);
            Log.e("CONTACT: ", contact + " " + number);
        }

        people.close();