2011年8月26日星期五

一個容易讀取手機聯繫人的方法, Activity.managedQuery()

本例子通過調用 Activity.managedQuery() 方法讀取手機聯繫人.

一個容易讀取手機聯繫人的方法, Activity.managedQuery()

package com.AndroidContacts;


import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.SimpleCursorAdapter;

public class AndroidContactsActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] projection = {People._ID, People.NAME};
Cursor cursor = managedQuery(
People.CONTENT_URI, //Uri
projection, //projection
null, //selection
null, //selection arguments
People.NAME); //Sort by

String[] name = new String[] {People.NAME};
int[] text1 = new int[] {android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
name,
text1);

setListAdapter(adapter);
}
}


最後, 需要修改 AndroidManifest.xml 文件, 添加 "android.permission.READ_CONTACTS" 權限


沒有留言:

發佈留言