1:首先实现读取联系人列表。
(1)MainActiivty.java
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c=managedQuery(People.CONTENT_URI,null,null,null,People.NAME);
String []cols=new String[]{People.NAME};
int[]views=new int[]{android.R.id.text1};
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,cols,views);
this.setListAdapter(adapter);
}
(2)AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS"/>
(3)运行结果
2:对联系人列表注册点击事件,实现打电话服务。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c=managedQuery(People.CONTENT_URI,null,null,null,People.NAME);
String []cols=new String[]{People.NAME};
int[]views=new int[]{android.R.id.text1};
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,cols,views);
this.setListAdapter(adapter);
ListView lv=getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Uri selectedPerson=ContentUris.withAppendedId(People.CONTENT_URI, arg3);
//这里不是直接拨通电话而是显示联系人页面
Intent intent=new Intent(Intent.ACTION_VIEW,selectedPerson);
startActivity(intent);
}
});
}
如: