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>
版权声明:本文为博主原创文章,未经博主允许不得转载。
android操作通讯录的联系人的更多相关文章
- Android向通讯录添加联系人的一般方法
Android向通讯录添加联系人的一般方法 以一个简单的例子加以说明,记得需要相应的权限: 测试代码,关键的内容就在add函数里面. package zhangphil.demo; import an ...
- Android项目——读取手机联系人信息
加入读取联系人信息的权限 <uses-permission android:name="android.permission.READ_CONTACTS"/> cont ...
- Android实现SQLite数据库联系人列表
Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...
- Android开发之读写联系人
读写联系人需要用到android的ContentProvider 同时需要读和写联系人的权限 需要使用到联系人数据库中的 * raw_contacts表: * contact_id:联系人id * d ...
- Android操作HTTP实现与服务器通信(转)
Android操作HTTP实现与服务器通信 本示例以Servlet为例,演示Android与Servlet的通信. 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方 ...
- android操作sdcard中的多媒体文件(二)——音乐列表的更新
android操作sdcard中的多媒体文件(二)——音乐列表的更新 原文地址 在上一篇随笔中,我介绍了如何在程序中查询sdcard内的多媒体文件,并且显示到播放列表中,但是,如果在sdcard内删除 ...
- android操作sdcard中的多媒体文件(一)——音乐列表的制作
android操作sdcard中的多媒体文件(一)——音乐列表的制作 原文地址 最近做了一个android音乐播放器,个人感觉最难的就是“后台播放”以及有关“播放列表”的部分,但是总算是找到了实现的方 ...
- [android] 获取系统的联系人信息
内容提供是实质上是个接口,后门,他给别人提供数据,系统联系人是个比较复杂的内容通过者. 找到/data/data/com.android.providers.contacts/contacts2.db ...
- android操作sqlite数据库及心得
写这篇文章主要是网上的对sqlite的操作太多且太杂,非常多时候都不能非常好的运用到自己的项目中,结构不清晰,我自己写了一篇适合刚刚接触的人看的操作方法. 近来用android时要将一些数据保存起来, ...
随机推荐
- 学习微软中间语言(MSIL)的绝佳工具 Dotnet IL Editor 推荐
Dotnet IL Editor是一款.NET平台反编译工具,可以反编译.NET程序集文件为IL代码,并且可以执行,调试反编译后生成的IL代码.它的设计出发点比较直观,新建一个项目,添加程序集文件,设 ...
- LINUX的一些常用操作
CentOs6.7关闭防火墙(SecureCRT连接不上) 解决方法:______________________________________一.开启SSH以root用户登录Linux,打开终端, ...
- [c++]堆和栈的区别
堆和栈的区别一.预备知识—程序的内存分配一个由c/C++编译的程序占用的内存分为以下几个部分1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构 ...
- 如何同时启动多个Tomcat服务
在项目开发中,有时会需要同时启动多个Tomcat服务,如果直接启动多个的话,会报以下错误: Port busy xxxx java.net.SocketException: Unrecognized ...
- Hdu 5568 sequence2 高精度 dp
sequence2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=556 ...
- URAL 1780 G - Gray Code 找规律
G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- HDU 4587 B - TWO NODES tarjan
B - TWO NODESTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- 使用GLSL实现更多数量的局部光照 【转】
原文 http://www.cnblogs.com/CGDeveloper/archive/2008/07/02/1233816.html 众所周知,OpenGL固定管线只提供了最多8盏灯光.如何使得 ...
- delphi 保存网页MHT
delphi 保存网页MHT uses ADODB_TLB, CDO_TLB, ComObj,MSHTML;{$R *.dfm}{能把网页如 WWW.QQ.COM保存为一个单文件 .MHT但不能把 ...
- True Zero Downtime HAProxy Reloads--转载
原文地址:http://engineeringblog.yelp.com/2015/04/true-zero-downtime-haproxy-reloads.html HAProxy: Corner ...