界面配置文件

<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操作通讯录的联系人的更多相关文章

  1. Android向通讯录添加联系人的一般方法

    Android向通讯录添加联系人的一般方法 以一个简单的例子加以说明,记得需要相应的权限: 测试代码,关键的内容就在add函数里面. package zhangphil.demo; import an ...

  2. Android项目——读取手机联系人信息

    加入读取联系人信息的权限 <uses-permission android:name="android.permission.READ_CONTACTS"/> cont ...

  3. Android实现SQLite数据库联系人列表

    Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...

  4. Android开发之读写联系人

    读写联系人需要用到android的ContentProvider 同时需要读和写联系人的权限 需要使用到联系人数据库中的 * raw_contacts表: * contact_id:联系人id * d ...

  5. Android操作HTTP实现与服务器通信(转)

    Android操作HTTP实现与服务器通信   本示例以Servlet为例,演示Android与Servlet的通信. 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方 ...

  6. android操作sdcard中的多媒体文件(二)——音乐列表的更新

    android操作sdcard中的多媒体文件(二)——音乐列表的更新 原文地址 在上一篇随笔中,我介绍了如何在程序中查询sdcard内的多媒体文件,并且显示到播放列表中,但是,如果在sdcard内删除 ...

  7. android操作sdcard中的多媒体文件(一)——音乐列表的制作

    android操作sdcard中的多媒体文件(一)——音乐列表的制作 原文地址 最近做了一个android音乐播放器,个人感觉最难的就是“后台播放”以及有关“播放列表”的部分,但是总算是找到了实现的方 ...

  8. [android] 获取系统的联系人信息

    内容提供是实质上是个接口,后门,他给别人提供数据,系统联系人是个比较复杂的内容通过者. 找到/data/data/com.android.providers.contacts/contacts2.db ...

  9. android操作sqlite数据库及心得

    写这篇文章主要是网上的对sqlite的操作太多且太杂,非常多时候都不能非常好的运用到自己的项目中,结构不清晰,我自己写了一篇适合刚刚接触的人看的操作方法. 近来用android时要将一些数据保存起来, ...

随机推荐

  1. CodeForces 732A Buy a Shovel (水题)

    题意:你手中有10元的钱,还有一个r元的零钱,要买一个价格为k的物品,但是你要求不找零钱,求最少要买多少物品. 析:直接暴力,最多买10个物品就够了1-10. 代码如下: #pragma commen ...

  2. 用ConfigurationManager读取和修改配置文件

    为了方便有时我们会把一些简单的配置的信息放入web.config文件里. 放到appSettings添加key   value等信息. ConfigurationManager.AppSettings ...

  3. Winform开发框架之通用自动更新模块(转)

    在网络化的环境中,特别是基于互联网发布的Winform程序,程序的自动更新功能是比较重要的操作,这样可以避免挨个给使用者打电话.发信息通知或者发送软件等,要求其对应用程序进行升级.实现程序的自动更新, ...

  4. WebView自适应并嵌套在ScrollView里

    大致思路:通过流的形式把网页抓取下来,然后对webView进行设置. 1.对webView进行设置 web.setWebViewClient(new WebViewClient() { @Overri ...

  5. flash的dragonbone插件导入cocos2d的注意事项

    一:Flash版本号应该为CS 6.0,低版本号不提供支持 二:新建flash项目的时候应该选择ActionScript3.0 三:动画中仅仅有两种元素,一个是"元件",还有一个是 ...

  6. 关于STM32的ST官方的库的一点看法

    标题确实很别扭,因为我现在用这个库也很别扭. 在不久之前,一直有个讨论的话题:STM32开发是用库还是用寄存器? 很多人的结论是不需要讨论! 但是,今天我想说下我的看法. 首先,我还是一个菜鸟,对库对 ...

  7. C++,C#,Python

    1.C++的思路:无论是基本类型,还是类类型,对象的传递提供了两种方式,一个是整体拷贝,一个是复制引用.整体拷贝对应着copy构造和copy赋值,复制引用就是通过引用或者指针实现的,当然指针本身还是整 ...

  8. [OSG]如何用Shader得到物体的世界坐标

    来自:http://www.cnblogs.com/hesicong/archive/2008/05/27/1208312.html 最近群里面有个朋友问我关于如何得到OpenGL世界坐标的问题,当时 ...

  9. C++ ADO 数据查询

    ADO 数据查询 关键点 上1条 下1条 第1条 最后1条 实现过程 // stdafx.h : include file for standard system include files, #im ...

  10. Online DDL与pt-online-schema-change

    http://seanlook.com/2016/05/24/mysql-online-ddl-concept/ http://seanlook.com/2016/05/27/mysql-pt-onl ...