Get a single phone number from contacts Android

允我心安 提交于 2019-12-04 14:38:43

When you are in your contacts list and click a Contact, you are going to get the Result of your Intent.ACTION_PICK intent.

So, to handle the result, you have to override onActivityResult() method.

You receive the lookup URI of your contact in the data Intent object returned by onActivityResult().

So, all you have to do is this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == CONTACT_PICKER_RESULT){
        if(resultCode==Activity.RESULT_OK){

            Uri uri = data.getData();
            ContentResolver contentResolver = getContentResolver();
            Cursor contentCursor = contentResolver.query(uri, null, null,null, null);

            if(contentCursor.moveToFirst()){
                String id = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

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

                if (hasPhone.equalsIgnoreCase("1")) 
                {
                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                    phones.moveToFirst();
                    String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i("phoneNUmber", "The phone number is "+ contactNumber);

                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

Then you just have to do whatever you want with the value on contactNumber.

Hope to have help you.

Muhammad Kashif Arif

First of all add this line in AndroidManifest.xml to take permission from user.

Mobile User also need to allow this permission.it can be done manually from settings

<uses-permission android:name="android.permission.READ_CONTACTS"/>

now in button Click listener write this code.

        contactsButtton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(contactPickerIntent,1);

            }
        });

Now from the outside of the onCreate() function. you have to override the onActivityResult() function. similar to this

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


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode){
        case 1 :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(contactData, null, null, null, null);
                if (cur.getCount() > 0) {// thats mean some resutl has been found
                    if(cur.moveToNext()) {
                        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Log.e("Names", name);
                        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.e("Number", phoneNumber);
                            }
                            phones.close();
                        }

                    }
                }
                cur.close();
            }
            break;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!