Android 获取手机联系人之方式二
2017-02-21 16:30
344 查看
首先先来看看效果图
第一步添加读取联系人权限
第二步 设计布局
activity_main.xml
3.编写Activity
MainActivity.java
简单适配器文件
item_simple_1.xml
第一步添加读取联系人权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
第二步 设计布局
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" tools:context="com.zking.mycontacts.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/list"></ListView> </LinearLayout>
3.编写Activity
MainActivity.java
package com.zking.mycontacts; import android.app.ListActivity; 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.widget.ListView; import android.widget.SimpleAdapter; import com.zking.entity.Phone; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.StringTokenizer; public class MainActivity extends ListActivity { private List<Phone> phones = new ArrayList<>(); private ContentResolver cr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { cr = getContentResolver(); phones = getPhones(); List<Map<String, Object>> maps = new ArrayList<>(); for (int i = 0; i < phones.size(); i++) { Phone phone = phones.get(i); Map<String, Object> map = new HashMap<>(); map.put("_id",phone.getId() + ""); map.put("name",phone.getName()); map.put("telPhone",phone.getTelPhone()); maps.add(map); } SimpleAdapter simpleAdapter = new SimpleAdapter(this,maps,R.layout.item_simple_1,new String[]{"_id","name","telPhone"},new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_telphone}); setListAdapter(simpleAdapter); } private List<Phone> getPhones(){ List<Phone> phones = new ArrayList<>(); Cursor cursor = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),null,null,null,null); while (cursor.moveToNext()){ Phone phone = new Phone(); int id = cursor.getInt(cursor.getColumnIndex("_id")); phone.setId(id); phone.setName(cursor.getString(cursor.getColumnIndex("display_name"))); Cursor cursor1 = cr.query(Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"),null,null,null,null); while (cursor1.moveToNext()){ String type = cursor1.getString(cursor1.getColumnIndex("mimetype")); if (type.contains("phone")){ phone.setTelPhone(cursor1.getString(cursor1.getColumnIndex("data1"))); } } phones.add(phone); } return phones; } }
简单适配器文件
item_simple_1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:id="@+id/tv_id"/> <TextView android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:id="@+id/tv_name" /> <TextView android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:id="@+id/tv_telphone"/> </LinearLayout>
相关文章推荐
- android 获取手机联系人信息
- Android基础教程(四)之------获取手机联系人信息
- Android2.0中获取手机里联系人的方法
- 如何获取android手机联系人并按字母展示(一)
- android获取手机联系人信息(电话,邮箱,姓名,头像)
- android手机两种方式获取IP地址
- android手机两种方式获取IP地址
- 如何获取android手机联系人并按字母展示(二)
- android手机两种方式获取IP地址
- android获取手机通讯录联系人
- Android获取手机联系人信息的方法
- Android 获取手机联系人信息
- Android应用学习记录------AsyncTask和AsyncQueryHandler结合获取手机联系人信息
- 如何获取android手机联系人并按字母展示(三)
- Android获取手机内联系人信息
- Android项目实战--手机卫士08--获取手机联系人
- android获取手机通讯录联系人
- android获取手机联系人并且按字母进行排序
- android手机如何获取某APP的页面耗时数据(命令行方式)
- android开发步步为营之11:从手机联系人列表获取联系电话