Android Read Contacts from Mobile Phone Address Book to Your Own Item

  • 2021-09-12 02:26:16
  • OfStack

This article example for everyone to share Android to read the mobile phone address book contact to the specific code of the project, for your reference, the specific content is as follows

1. The main interface code is as follows:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <ListView
  android:id="@+id/contacts_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

 </ListView>
</LinearLayout>

Simply add an listview to show the address book data to be read later.

2. The MainAcitivity code is as follows, with detailed comments in the code!


public class MainActivity extends AppCompatActivity {

 ArrayAdapter<String> adapter;

 List<String> contactsList=new ArrayList<>();

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

  // Get to listview And set up the adapter 
  ListView contactsView= (ListView) findViewById(R.id.contacts_view);
  adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);
  contactsView.setAdapter(adapter);

  // Determine whether to turn on the permission to read the address book 
  if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager
    .PERMISSION_GRANTED){
   ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
  }else {
   readContacts();
  }
 }

 private void readContacts() {
  Cursor cursor=null;
  try {
   // Query contact data , Used getContentResolver().query Method to query the data of contacts of the system 
   //CONTENT_URI Is 1 A packaged one Uri Is a constant that has been resolved 
   cursor=getContentResolver().query(
     ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
     null,
     null,
     null,
     null
   );
   // Right cursor Perform a traversal to retrieve the name and phone number 
   if (cursor!=null){
    while (cursor.moveToNext()){
     // Get the contact name 
     String displayName=cursor.getString(cursor.getColumnIndex(
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
     ));
     // Get the contact's mobile phone number 
     String number=cursor.getString(cursor.getColumnIndex(
       ContactsContract.CommonDataKinds.Phone.NUMBER
     ));
     // Splice the two types of data taken out, add a newline character in the middle, and then add it to listview Medium 
     contactsList.add(displayName+"\n"+number);
    }
    // Refresh listview
    adapter.notifyDataSetChanged();
   }
  }catch (Exception e){
   e.printStackTrace();
  }finally {
   // Remember to turn it off cursor
   if (cursor!=null){
    cursor.close();
   }
  }
 }

 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  switch (requestCode){
   case 1:
    if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
     readContacts();
    }else {
     Toast.makeText(this," No permissions ",Toast.LENGTH_SHORT).show();
    }
    break;
   default:
    break;
  }
 }
}

3. Because reading the address book is a dangerous permission, remember to turn on the permission in Manifest


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

Ok, run 1 below, and you can read out the address book data in your mobile phone!


Related articles: