Android获取联系人信息 Android利用ContentProvider获取联系人信息
安之若素i 人气:0想了解Android利用ContentProvider获取联系人信息的相关内容吗,安之若素i在本文为您仔细讲解Android获取联系人信息的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ContentProvider获取联系人信息,Android获取联系人,Android获取信息,下面大家一起来学习吧。
在写代码前我们首先看一下运行的效果
运行效果如下:
点了获取联系人就展示如下效果
读取联系人信息的例子(MainActivity)
package com.example.administrator.myapplication; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; /** * 获取联系人 */ public class MainActivity extends AppCompatActivity { private ContentResolver cr; private List<Map<String,Object>> data; private ListView lv_main_list; private SimpleAdapter simpleAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_main_list = (ListView)findViewById(R.id.lv_main_list); //获取内容访问者 cr = getContentResolver(); data = new ArrayList<Map<String,Object>>(); //适配器 simpleAdapter = new SimpleAdapter(this,data,android.R.layout.simple_list_item_2,new String[]{"names","phones"},new int[]{android.R.id.text1,android.R.id.text2}); lv_main_list.setAdapter(simpleAdapter); } public void getContacts(View view){ Cursor cursor=cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),null,null,null,null); while(cursor.moveToNext()){ Map<String,Object> map=new HashMap<String,Object>(); int id=cursor.getInt(cursor.getColumnIndex("_id")); String displayName=cursor.getString(cursor.getColumnIndex("display_name")); Log.i("test",id+" "+displayName); map.put("names",displayName); //根据联系人获取联系人数据 Cursor cursor2=cr.query(Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"),null,null,null,null); while(cursor2.moveToNext()){ // int type=cursor2.getInt(cursor2.getColumnIndex("mimetype_id")); String type=cursor2.getString(cursor2.getColumnIndex("mimetype")); String data1=null; if ("vnd.android.cursor.item/phone_v2".equals(type)){ data1 = cursor2.getString(cursor2.getColumnIndex("data1")); Log.i("test"," "+type+" "+data1); map.put("phones",data1); } } data.add(map); } //通知适配器发生改变 simpleAdapter.notifyDataSetChanged(); } }
布局文件(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.administrator.myapplication.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取所有的联系人" android:onClick="getContacts" /> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_main_list" ></ListView> </LinearLayout>
需要在清单文件上配置联系人的权限(AndroidManifest.xml)
<uses-permission android:name="android.permission.READ_CONTACTS" />
加载全部内容