Android利用ContentProvider獲取聯(lián)系人信息
本文實(shí)例為大家分享了Android利用ContentProvider獲取聯(lián)系人信息的具體代碼,供大家參考,具體內(nèi)容如下
在寫代碼前我們首先看一下運(yùn)行的效果
運(yùn)行效果如下:
點(diǎn)了獲取聯(lián)系人就展示如下效果
讀取聯(lián)系人信息的例子(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; /** * 獲取聯(lián)系人 */ 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); //獲取內(nèi)容訪問者 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); //根據(jù)聯(lián)系人獲取聯(lián)系人數(shù)據(jù) 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); } //通知適配器發(fā)生改變 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="獲取所有的聯(lián)系人" android:onClick="getContacts" /> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_main_list" ></ListView> </LinearLayout>
需要在清單文件上配置聯(lián)系人的權(quán)限(AndroidManifest.xml)
<uses-permission android:name="android.permission.READ_CONTACTS" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)拖動(dòng)小球跟隨手指移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拖動(dòng)小球跟隨手指移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03微信小程序 canvas開發(fā)實(shí)例及注意事項(xiàng)
這篇文章主要介紹了微信小程序 wxcanvas開發(fā)實(shí)例及注意事項(xiàng)的相關(guān)資料,這里對(duì)微信canvas與H5中的canvas做對(duì)比,并說明注意事項(xiàng),需要的朋友可以參考下2016-12-12Android游戲開發(fā)學(xué)習(xí)之引擎用法實(shí)例詳解
這篇文章主要介紹了Android游戲開發(fā)學(xué)習(xí)之引擎用法,較為詳細(xì)的分析了Android游戲開發(fā)中所常用的JBox2D引擎功能及相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android 動(dòng)畫之AlphaAnimation應(yīng)用詳解
本節(jié)講解AlphaAnimation 動(dòng)畫,窗口的動(dòng)畫效果,淡入淡出什么的,有些游戲的歡迎動(dòng)畫,logo的淡入淡出效果就使用AlphaAnimation,具體的祥看本文,需要的朋友可以參考下2012-12-12Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法,實(shí)例總結(jié)了屏幕旋轉(zhuǎn)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04android開發(fā)實(shí)現(xiàn)文件讀寫
這篇文章主要為大家詳細(xì)介紹了android開發(fā)實(shí)現(xiàn)文件讀寫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07限時(shí)搶購秒殺系統(tǒng)架構(gòu)分析與實(shí)戰(zhàn)
這篇文章主要介紹了限時(shí)搶購秒殺系統(tǒng)架構(gòu)分析與實(shí)戰(zhàn) 的相關(guān)資料,需要的朋友可以參考下2016-01-01Android自定義控件深入學(xué)習(xí) Android生成隨機(jī)驗(yàn)證碼
這篇文章主要再次為大家介紹了Android自定義控件,以及針對(duì)自定義view學(xué)習(xí),實(shí)戰(zhàn)演練了Android生成隨機(jī)驗(yàn)證碼的詳細(xì)過程,感興趣的小伙伴們可以參考一下2016-01-01