Android利用ContentProvider获取联系人信息
程序员文章站
2022-06-16 21:29:09
本文实例为大家分享了android利用contentprovider获取联系人信息的具体代码,供大家参考,具体内容如下在写代码前我们首先看一下运行的效果运行效果如下:点了获取联系人就展示如下效果读取联...
本文实例为大家分享了android利用contentprovider获取联系人信息的具体代码,供大家参考,具体内容如下
在写代码前我们首先看一下运行的效果
运行效果如下:
点了获取联系人就展示如下效果
读取联系人信息的例子(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" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。