android利用ContentResolver訪問者獲取手機(jī)聯(lián)系人信息
利用ContentResolver內(nèi)容訪問者,獲取手機(jī)聯(lián)系人信息我做了兩種不同的做法。第一種,直接獲取所有手機(jī)聯(lián)系人信息,展示在ListView中。第二種,通過Butten按鈕跳轉(zhuǎn)到系統(tǒng)的手機(jī)聯(lián)系人界面,單個(gè)獲取手機(jī)聯(lián)系人信息,展示在ListView中,結(jié)果如下:
第一種:

第二種:

第一種:直接獲取所有手機(jī)聯(lián)系人信息
首先需要在AndroidManifest.xml文件中添加權(quán)限:
<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();
//定義一個(gè)接收聯(lián)系人姓名和電話號(hào)碼的集合
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="";
//用來裝號(hào)碼
String bb="";
while(contactData.moveToNext()){
String type=contactData.getString(contactData.getColumnIndex("mimetype"));
//如果獲取的是vnd.android.cursor.item/phone_v2則是號(hào)碼
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"));
}
}
//將用戶名和號(hào)碼放入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按鈕跳轉(zhuǎn)到系統(tǒng)的手機(jī)聯(lián)系人界面,單個(gè)獲取手機(jī)聯(lián)系人信息,展示在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="跳轉(zhuǎn)到聯(lián)系人頁面"
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);
//獲得跳轉(zhuǎn)到聯(lián)系人的id
b_tzcontacts =(Button) findViewById(R.id.b_tzcontacts);
//獲得ListView的ID
lv_contacts =(ListView) findViewById(R.id.lv_contacts);
//定義一個(gè)接受聯(lián)系人姓名和電話號(hào)碼的集合
datalistView = new ArrayList<>();
//獲取聯(lián)系人的點(diǎn)擊事件
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布局問價(jià)
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);
}
//獲得返回的結(jié)果
@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()){
//聯(lián)系人
phoneName =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//手機(jī)號(hào)碼
phoneNumber =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//格式化手機(jī)號(hào)
phoneNumber = phoneNumber.replace("-","");
phoneNumber = phoneNumber.replace("","");
//將用戶名和號(hào)碼放入Map集合中
Map<String,Object> map=new HashMap<>();
map.put("images",phoneName);
map.put("titles",phoneNumber);
datalistView.add(map);
}
//刷新適配器
adapter.notifyDataSetChanged();
}
break;
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
- Android 獲取手機(jī)聯(lián)系人實(shí)例代碼詳解
- Android獲取手機(jī)聯(lián)系人信息
- Android獲取手機(jī)聯(lián)系人電話號(hào)碼并返回結(jié)果
- Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
- android如何獲取手機(jī)聯(lián)系人的數(shù)據(jù)庫示例代碼
- Android編程實(shí)現(xiàn)讀取手機(jī)聯(lián)系人、撥號(hào)、發(fā)送短信及長按菜單操作方法實(shí)例小結(jié)
- 淺談Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能
- Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例
- Android ContentProvider實(shí)現(xiàn)手機(jī)聯(lián)系人讀取和插入
相關(guān)文章
android實(shí)用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機(jī)分辨率)
這篇文章主要介紹了android實(shí)用工具類,包括獲取內(nèi)存、檢查網(wǎng)絡(luò)、屏幕高度、手機(jī)分辨率、獲取版本號(hào)等功能,需要的朋友可以參考下2014-03-03
通過實(shí)例簡單講解Android App中的Activity組件
這篇文章主要介紹了通過Android App中的Activity組件,包括Activity的定義和繼承以及啟動(dòng)等基本知識(shí),需要的朋友可以參考下2016-04-04
Kotlin中的惰性操作容器Sequence序列使用原理詳解
這篇文章主要為大家介紹了Kotlin中的惰性操作容器Sequence序列使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Kotlin使用滾動(dòng)控件RecyclerView實(shí)例教程
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來講解RecyclerView的用法2022-12-12
Android之仿美團(tuán)加載數(shù)據(jù)幀動(dòng)畫
本文主要介紹了Android仿美團(tuán)加載數(shù)據(jù)幀動(dòng)畫的實(shí)例方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
Android TouchListener實(shí)現(xiàn)拖拽刪實(shí)例代碼
這篇文章主要介紹了Android TouchListener實(shí)現(xiàn)拖拽刪實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02

