如何只接触手机号码

时间:2016-08-09 11:26:17

标签: java android android-contacts phone-number

我有以下代码来获取联系人姓名和号码。如何只联系手机号码和姓名?联系人姓名可能包含一些数字。如何获取名称的手机号码?

 ContentResolver cr = activity.getContentResolver();
 Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
 while (phones.moveToNext()) {
    name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 }

3 个答案:

答案 0 :(得分:0)

尝试使用此代码。为我工作。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    Button orderButton = (Button) findViewById(R.id.button3);
    Button exit = (Button) findViewById(R.id.button2);
    exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
 }    

答案 1 :(得分:0)

您可以查询ContactsContract.CommonDataKinds.Phone.TYPE列以确定数字的类型:

int type = phones.getInt(phones.
    getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

if(type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
    phoneNumber = phones.getString(phones.
        getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}

答案 2 :(得分:0)

 ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                       if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
            {
                // Query phone here. Covered next
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 
                while (phones.moveToNext()) { 
                         String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Log.i("Number", phoneNumber);
                        } 
                phones.close(); 
            }

        }
    }

您必须要求此权限:

  

uses-permission android:name =" android.permission.READ_CONTACTS"