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时要将一些数据保存起来, ...
随机推荐
- 分析恶意驱动(进程启动apc注入dll)
一.前言 用IDA也有好些时间了,以前就只会用F5功能玩无壳无保护的裸驱动,感觉太坑了,这两天就开始看网上大牛的逆向. 今天记录一下sudami曾经逆向过的fuck.sys.第一遍自己走的时候漏掉了 ...
- SOS 调试扩展 (SOS.dll)
http://blog.csdn.net/cslie/article/details/2158780 SOS 调试扩展 (SOS.dll) 提供公共语言运行时(CLR)内部环境的有关信息,帮助你在Wi ...
- SQL 错误1418
1.一个或多个服务器网络地址缺少完全限定域名(FQDN).为每个服务器指定FQDN,然后再次单击“开始镜像”.2.服务器网络地址"TCP://primary.test.com:5022&qu ...
- 比较器comparable与comparator的使用
在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...
- PHP+MySQL多语句执行<转自wooyun>
发起这个帖子,估计就很多人看到题目就表示不屑了.一直以来PHP+MySQL环境下,无论是写程序或者是注入攻击,是无法多语句执行的,这么广为人知的常识,没理由会有人不知道.可权威就是用来被挑战的,常识也 ...
- 【转】移动端App测试实用指南
转自:互联网那点事 英文原文: http://mobile.smashingmagazine.com/2012/10/22/a-guide-to-mobile-app-testing/ 测试人员常被看 ...
- 从零开始学android开发-用Intent启动Activity的方法
启动另外一个Activity,可以有的方法有用setClass()和Component Name 1. 先说在setClass启动一个Activity的方法吧: Intent intent = new ...
- [Mapreduce]eclipse下写wordcount
上传两个文件到hdfs上的input目录下 代码例如以下: import java.io.IOException; import java.util.StringTokenizer; import o ...
- 几种server模型
TCP測试用客户程序 每次执行客户程序,在命令行參数指定server的ip地址,port,发起连接的子进程数,和一个待发送的字符串数据,客户程序将模拟多个客户依据指定的子进程数创建子进程来并发的连接到 ...
- Android腾讯微博开发之随机字符串与签名实现
Android腾讯微博开发入门之随机字符串与签名实现 直接上代码 1.Utils类,包括签名和随机字符串 import java.util.Random; import javax.cry ...