android操作通讯录的联系人
界面配置文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text"
android:textSize="30px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="手机联系人列表"/>
<ListView
android:id="@+id/contactslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
SimpleAdapter的配置文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/_id"
android:textSize="30px"
android:layout_width="240px"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/name"
android:textSize="30px"
android:layout_width="240px"
android:layout_height="wrap_content"/>
</LinearLayout>
Acitivity程序
package com.example.contactproject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView contactslist=null;
private SimpleAdapter simpleadapter=null;
private Cursor result=null;
private List<Map<String, Object>> allContacts=new ArrayList<Map<String,Object>>();
@Override
public boolean onContextItemSelected(MenuItem item) {//选中某个菜单项
AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)
item.getMenuInfo();//取得菜单项
int position=info.position;//取得操作ID
String contactsId=this.allContacts.get(position).get("_id").toString();//取得数据ID
switch(item.getItemId()){//判断菜单项ID
case Menu.FIRST+1://查询数据
String phoneSelection=ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?";//设置查询条件
String[] phoneSelectionAras=new String[]{contactsId}; //查询参数
Cursor result=super.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, phoneSelection, phoneSelectionAras, null);//获得查询的手机号码
StringBuffer buf=new StringBuffer();//用于接受号码
buf.append("电话号码是:");
for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){//循环取出数据
buf.append(result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+"\\");
}
Toast.makeText(this, buf.toString(), Toast.LENGTH_LONG).show();
break;
case Menu.FIRST+2://删除数据
super.getContentResolver().delete(Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI,
contactsId),
null, null);//删除指定数据
this.allContacts.remove(position);//删除数据项
this.simpleadapter.notifyDataSetChanged();//更新列表项
Toast.makeText(this, "数据已经删除", Toast.LENGTH_LONG).show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {//创建上下文菜单
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("联系人操作");
menu.add(Menu.NONE, Menu.FIRST+1, 1, "查看详情");
menu.add(Menu.NONE, Menu.FIRST+2, 2, "删除联系人");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.contactslist=(ListView)super.findViewById(R.id.contactslist);
this.result=super.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
super.startManagingCursor(result);
for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){
Map<String,Object> map=new HashMap<String, Object>();
map.put("_id",result.getInt(result.getColumnIndex(
ContactsContract.Contacts._ID)));
map.put("name", result.getString(result.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)));
this.allContacts.add(map);
}
this.simpleadapter=new SimpleAdapter(
this, this.allContacts,
R.layout.contacts,
new String[]{"_id","name"},
new int[]{R.id._id,R.id.name});
this.contactslist.setAdapter(this.simpleadapter);
this.registerForContextMenu(this.contactslist);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
权限设置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.contactproject.MainActivity"
android:label="@string/app_name" >
<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>