联系人是一个单独的程序,可以通过ContentProvider访问联系人信息。
ContenProvider可以将一个应用程序的数据交给外部处理,
使用SimpleCursorAdapter类可以对Curosr进行封装,并组件显示。
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#000000">
<TextView
android:id="@+id/mainInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="手机联系人列表"/>
<ListView
android:id="@+id/contactList"
android:background="#0066ff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
新建布局文件contacts.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0066ff">
<TableRow >
<TextView
android:id="@+id/_id"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"/>
<TextView
android:id="@+id/name"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="8dp"/>
</TableRow>
</TableLayout>
在主Activity(ContentsDemo.java)中:
package com.li.contactsproject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.support.v4.app.NavUtils;
public class ContentsDemo extends Activity {
private Cursor result = null; //查询返回的结果
private ListView contactsList = null; //定义ListView组件
private List<Map<String,Object>> allContacts = null;
private SimpleAdapter simple = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.contactsList = (ListView)super.findViewById(R.id.contactList);
this.result = super.getContentResolver().query(ContactsContract
.Contacts.CONTENT_URI, null, null, null, null); //查询
super.startManagingCursor(this.result); //将结果集交给容器管理
this.allContacts = new ArrayList<Map<String,Object>>(); //实例化List集合
for(this.result.moveToFirst();!this.result.isAfterLast();
this.result.moveToNext()){ //取出结果集中的每一个内容
Map<String,Object> contact = new HashMap<String, Object>();
contact.put("_id",this.result.getInt(this.result
.getColumnIndex(ContactsContract.Contacts._ID)));
contact.put("name",this.result.getString(this.result
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
this.allContacts.add(contact);
this.simple = new SimpleAdapter(this,this.allContacts,
R.layout.contacts,new String[]{"_id","name"},new int[]{
R.id._id,R.id.name});
this.contactsList.setAdapter(this.simple);
}
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.contactsproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ContentsDemo"
android:label="@string/title_activity_contents_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
</manifest>