ContentResolver(内容访问者)访问通讯录
来源:互联网 发布:学英语软件 编辑:程序博客网 时间:2024/06/04 06:44
访问通讯录可以分为两种方式,第一种是直接调用系统通讯录的界面直接访问,第二种是自己写一个界面,从系统通讯录里面拿到数据,再绑定在自己写的界面。
今天我们用第二种方法来实现访问通讯录
1,访问通讯录首先要写访问通讯录的权限
<!--访问通讯录的权限--> <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
2,拿到内容访问者
//得到内容访问者 ContentResolver cr= getContentResolver();
3,用内容访问者去点 query 方法,返回类型是Cursor
Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");Cursor cursor=cr.query(uri,null,null,null,null);
4,得到ID和名字
while (cursor.moveToNext()){ int id=cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("display_name"));// Log.i("text",""+id+" "+name);}
5,根据联系人的ID获取联系人相对应的数据(名字+phone)
Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"); Cursor cursor1=cr.query(uriData,null,null,null,null); while(cursor1.moveToNext()){ //号码 String data1=cursor1.getString(cursor1.getColumnIndex("data1")); //int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id")); String type=cursor1.getString(cursor1.getColumnIndex("mimetype")); if("vnd.android.cursor.item/phone_v2".equals(type)){ Log.i("text"," "+data1+" "+ name); } }
然后自己在写一个ListView把数据放进去就OK了
package com.zking.hgz_android_25_contentproviter_02;import android.content.ContentResolver;import android.database.Cursor;import android.net.Uri;import android.support.v4.widget.SimpleCursorAdapter;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.Adapter;import android.widget.BaseAdapter;import android.widget.ListView;public class Contacts extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contacts); ListView lv_contacts= (ListView) findViewById(R.id.lv_contacts); //得到内容访问者 ContentResolver cr= getContentResolver(); 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")); String name = cursor.getString(cursor.getColumnIndex("display_name"));// Log.i("text",""+id+" "+name); //根据联系人的ID获取联系人相对应的数据(名字+phone) Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"); Cursor cursor1=cr.query(uriData,null,null,null,null); while(cursor1.moveToNext()){ //号码 String data1=cursor1.getString(cursor1.getColumnIndex("data1")); //int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id")); String type=cursor1.getString(cursor1.getColumnIndex("mimetype")); if("vnd.android.cursor.item/phone_v2".equals(type)){ Log.i("text"," "+data1+" "+ name); } }// SimpleCursorAdapter adapter = new SimpleCursorAdapter(Contacts.this, R.layout.listview_item, cursor, new String[]{"display_name"}, new int[]{R.id.load2_listview_item_textview});// lv_contacts.setAdapter(adapter); } }}
0 0
- ContentResolver(内容访问者)访问通讯录
- ContentResolver 内容访问者访问手机联系人
- ContentResolver内容接收者访问系统通讯录
- android-内容访问者contentResolver
- Android-ContentResolver内容访问者
- ContentResolver(内容访问者)
- ContentResolver访问通讯录
- android之ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者 和 ContentResolver内容访问者
- Android----ContentProvider(内容提供者) ContentResolver(内容访问者)
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- 使用ContentResolver访问通讯录ContentProvider
- 使用ContentResolver访问通讯录ContentProvider
- HDU - 1198
- 伪静态URLRewrite访问路径加密
- 搭建lamp环境(CentOS7+Apache+MySQL+PHP)
- 高数Umaru系列(3)——喵星人
- 通过heat创建stack的代码流程分析heat stack-create
- ContentResolver(内容访问者)访问通讯录
- Java HttpUrlConnection多线程下载
- java语言都有哪些优点
- Android View
- ffmpeg--解析h264
- [省选] [线段树] [HLOI2016] 字符串问题
- postgresql字符串函数
- springmvc 配置多视图(jsp,freemarker,HTML等)
- Android四大组件之ContentProvider(内容提供者)02