利用ContentResolver内容k">访问者,获取手机联系人信息我做了两种不同的做法。第一种,直接获取所有手机联系人信息,展示在ListView中。第二种,通过Butten按钮跳转到系统的手机联系人界面,单个获取手机联系人信息,展示在ListView中,结果如下:

第一种:

第二种:

第一种:直接获取所有手机联系人信息

首先需要在AndroidManifest.xml文件中添加权限:

<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布局:

<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类:

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);
  //获得ListView
  lv_lxr = (ListView) findViewById(R.id.lv_lxr);
  //得到访问者
  cr = getContentResolver();
  //定义一个接收联系人姓名和电话号码的集合
  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);
        //用来装姓名
        String aa="";
        //用来装号码
        String bb="";
        while(contactData.moveToNext()){
          String type=contactData.getString(contactData.getColumnIndex("mimetype"));
          //如果获取的是vnd.android.cursor.item/phone_v2则是号码
          if(type.equals("vnd.android.cursor.item/phone_v2")){
            bb=contactData.getString(contactData.getColumnIndex("data1"));
            //如果获取的是vnd.android.cursor.item/name则是姓名
          }else if(type.equals("vnd.android.cursor.item/name")) {
            aa=contactData.getString(contactData.getColumnIndex("data1"));
          }
        }
        //将用户名和号码放入Map集合中
        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);
}

第二种:通过Butten按钮跳转到系统的手机联系人界面,单个获取手机联系人信息,展示在ListView中

activity_contacts.xml布局:

<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="跳转到联系人页面"
          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类:

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);
  //获得跳转到联系人的id
  b_tzcontacts =(Button) findViewById(R.id.b_tzcontacts);
  //获得ListView的ID
  lv_contacts =(ListView) findViewById(R.id.lv_contacts);
  //定义一个接受联系人姓名和电话号码的集合
  datalistView = new ArrayList<>();
  //获取联系人的点击事件
  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就是上文的activity_xs布局问价
  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){
        Uri uri=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("","");
          //将用户名和号码放入Map集合中
          Map<String,Object> map=new HashMap<>();
          map.put("images",phoneName);
          map.put("titles",phoneNumber);
          datalistView.add(map);
        }
        //刷新适配器
        adapter.notifyDataSetChanged();
      }
      break;
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。