Custom Search

How to pick an Email from contact in android

Picking email, Phone number, and other detail from android in built contact is very easy if you use ACTION_PICK.Action Pick activity syntax will be like


Intent intent1=new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent1,100);


Note : it need permission .so do not forget to mention it in manifest

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

It will start a default activity that will list all contact. On item select that activity will finish automatically and it will return result in OnActivityResult. default activity return complete Intent with all available information with particular select contact.



The main issue is now to handle Intent result in OnActivityResult. So now we will handle Intent



if(requestCode==100){
try{
if(resultCode==Activity.RESULT_OK){
Uri uri=data.getData();
String[] projection = new String[] {
ContactsContract.Contacts._ID,ontactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
Cursor emailCur=getContentResolver().query(uri,null, null, null,null);
emailCur.moveToFirst();
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.i("dddd",""+email+"djdjdj"+emailType);
emailCur.close();
}
catch(Exception e){
e.getCause();
}
}
}


                                    Download Sample

0 comments:

Post a Comment