您的位置:首页 > 移动开发 > Android开发

Android 获取手机联系人之方式二

2017-02-21 16:30 344 查看
首先先来看看效果图



第一步添加读取联系人权限

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: