Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • getting contact information from contact list in Android

    • 0
    • 4
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 530
    Comment on it

    We can get a contact information for any contact from contact list of an Android Device easily. For this follow the steps below:

    1. Write the permission to read Contact's data into your AndroidManifest.xml

      1. <uses-permission android:name="android.permission.READ_CONTACTS" />
    2. Write the below code within your activity from where you want to perform action.

      1. Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
      2. startActivityForResult(contactPickerIntent, 1001);
    3. Also in your Activity, write the below code inside the onActivityResult method to listen for the result of step 2.

      1. if(resultCode==RESULT_OK && requestCode==1001)
      2. {
      3. String ContactInfo = null;
      4. Uri uriOfPhoneNumberRecord = data.getData();
      5. String idOfPhoneRecord = uriOfPhoneNumberRecord.getLastPathSegment();
      6. Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{idOfPhoneRecord}, null);
      7.  
      8. if(cursor != null) {
      9. if(cursor.getCount() > 0) {
      10. cursor.moveToFirst();
      11. String formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA) );
      12. String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
      13.  
      14.  
      15. StringBuffer s=new StringBuffer(formattedPhoneNumber);
      16. for(int j=0;j<s.length();j++){
      17. if(s.charAt(j)=='-')
      18. {
      19. s.replace(j, j+1, "");
      20. }
      21. }
      22. ContactInfo = "Contact:\n".concat(contactName.concat("\n"+s.toString()));
      23. Log.d("Varta", String.format("The selected phone number is: %s", ContactInfo));
      24. }
      25. cursor.close();
      26. }
      27. }

    Please feel free to share the experiences you had while getting contact information from contact list in your code as this will help other people who read the post.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: