Android uses ContentResolver visitors to obtain mobile phone contact information

Reprinted from: http://www.jb51.net/article/106379.htm

 

 

 

First you need to add permissions to the AndroidManifest.xml file:

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

 

activity_main.xml布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_25.MainActivity">
 
    <ListView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/lv_lxr"
      >
    </ListView>
  </LinearLayout>

 

activity_xs.xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_xs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_25.XsActivity">
  <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/tv_name"
    />
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/tv_telephone"
    />
  </LinearLayout>

MainActivity class:

private ListView lv_lxr;
private Button b_name;
private ContentResolver cr;
private List<Map<String, Object>> datalistView;
 
@Override
protected  void onCreate (Bundle savedInstanceState) {
   super .onCreate (savedInstanceState);
  setContentView(R.layout.activity_main);
  // Get ListView 
  lv_lxr = (ListView) findViewById(R.id.lv_lxr);
   // Get visitors 
  cr = getContentResolver();
   // Define a collection that receives contact names and phone numbers 
  datalistView = new ArrayList<> () ;
      Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
      Cursor cursor= cr.query(uri,null,null,null,null);
      while(cursor.moveToNext()){
        int id=cursor.getInt(cursor.getColumnIndex("_id"));
        Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
        Cursor contactData = cr.query(uriData, null , null , null , null );
         // used to hold name 
        String aa="" ​​;
         // used to hold number 
        String bb="" ;
         while (contactData.moveToNext()) {
          String type =contactData.getString(contactData.getColumnIndex("mimetype" ));
           // If vnd.android.cursor.item/phone_v2 is obtained, it is the number 
          if (type.equals("vnd.android.cursor.item/ phone_v2" )){
            bb =contactData.getString(contactData.getColumnIndex("data1" ));
             // If vnd.android.cursor.item/name is obtained, it is the name 
          } else  if (type.equals("vnd.android.cursor.item /name" )) {
            aa=contactData.getString(contactData.getColumnIndex("data1"));
          }
        }
        // Put the username and number into the Map collection 
        Map<String,Object> map= new HashMap<> ();
        map.put("images",aa);
        map.put("titles",bb);
        datalistView.add(map);
      }
  SimpleAdapter adapter=new SimpleAdapter(this, datalistView,R.layout.activity_xs,new String[]{"images","titles"},new int[]{R.id.tv_name,R.id.tv_telephone});
  lv_lxr.setAdapter(adapter);
}

The second: Jump to the system's mobile phone contact interface through the Butten button, obtain the mobile phone contact information individually, and display it in the ListView

activity_contacts.xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_contacts"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context="com.example.android_25.ContactsActivity">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
        <Button
          android:layout_width = "wrap_content" 
          android:layout_height = "wrap_content" 
          android:text = "Jump to the contact page" 
          android:id = "@+id/b_tzcontacts"
          />
    </LinearLayout>
    <ListView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
        android:id="@+id/lv_contacts"
      ></ListView>
</LinearLayout>

ContactsActivity class:

private Button b_tzcontacts;
private String phoneName;
private String phoneNumber;
private List<Map<String,Object>> datalistView;
private ListView lv_contacts;
private SimpleAdapter adapter;
 
@Override
protected  void onCreate (Bundle savedInstanceState) {
   super .onCreate (savedInstanceState);
  setContentView(R.layout.activity_contacts);
  // Get the id that jumps to the contact 
  b_tzcontacts = (Button) findViewById(R.id.b_tzcontacts);
   // Get the ID of the ListView 
  lv_contacts = (ListView) findViewById(R.id.lv_contacts);
   // Define an accept contact A collection of people's names and phone numbers 
  datalistView = new ArrayList<> ();
   // Get the click event of a contact 
  b_tzcontacts.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      Intent intentPhone=new Intent(Intent.ACTION_PICK);
      intentPhone.setData(ContactsContract.Contacts.CONTENT_URI);
      startActivityForResult(intentPhone,0);
    }
  });
  // R.layout.activity_xs is the activity_xs layout above 
  adapter = new SimpleAdapter( this , datalistView, R.layout.activity_xs, new String[]{"images","titles"}, new  int []{R .id.tv_name,R.id.tv_telephone});
  lv_contacts.setAdapter(adapter);
}
 
 
  //获得返回的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch (requestCode){
    case 0:
      if(resultCode== Activity.RESULT_OK){
        Are you = data.getData ();
        Cursor cursor=managedQuery(uri,null,null,null,null);
        cursor.moveToFirst();
        String contactid=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        //得到ContentResolver
        ContentResolver contentResolver=getContentResolver();
        Cursor phone=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactid,null,null);
        while (phone.moveToNext()){
          //联系人
          phoneName =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          //手机号码
          phoneNumber =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          //格式化手机号
          phoneNumber = phoneNumber.replace("-","");
          phoneNumber = phoneNumber.replace("","" );
           // Put the username and number into the Map collection 
          Map<String,Object> map= new HashMap<> ();
          map.put("images",phoneName);
          map.put("titles",phoneNumber);
          datalistView.add(map);
        }
        // Refresh adapter 
        adapter.notifyDataSetChanged();
      }
      break;
  }
 
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325322250&siteId=291194637