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
    • 472
    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

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

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

      if(resultCode==RESULT_OK && requestCode==1001)
      {
      String ContactInfo = null;
      Uri uriOfPhoneNumberRecord = data.getData();
      String idOfPhoneRecord = uriOfPhoneNumberRecord.getLastPathSegment();
      Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{idOfPhoneRecord}, null);
      
      if(cursor != null) {
          if(cursor.getCount() > 0) {
              cursor.moveToFirst();
              String formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA) );
              String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
      
      
      StringBuffer s=new StringBuffer(formattedPhoneNumber);
          for(int j=0;j<s.length();j++){
              if(s.charAt(j)=='-')
              {
                  s.replace(j, j+1, "");
              }
          }
          ContactInfo = "Contact:\n".concat(contactName.concat("\n"+s.toString()));
          Log.d("Varta", String.format("The selected phone number is: %s", ContactInfo));
      }
      cursor.close();  
      }
      }

    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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: