Android中的Contacts联系人通过ContentProvider对外提供联系人数据及操作接口。
下面通过简单例子,来看看有关联系人的基本操作。
主Activity: 001 | public class MainActivity extends ListActivity { |
002 |
private ArrayList<String> list = new ArrayList<String>(); |
003 |
private ArrayAdapter adapter; |
004 |
/** Called when the activity is first created. */ |
006 |
public void onCreate(Bundle savedInstanceState) { |
007 |
super .onCreate(savedInstanceState); |
011 |
adapter = new ArrayAdapter( this ,R.layout.main,list); |
012 |
this .setListAdapter(adapter); |
015 |
public void queryAll() { |
016 |
ContentResolver resolver = this .getContentResolver(); |
017 |
Uri uri = Uri.parse( "content://com.android.contacts/contacts" ); |
018 |
Cursor idCursor = resolver.query(uri, new String[] { "_id" }, null , null , null ); |
019 |
while (idCursor.moveToNext()) { |
021 |
int id = idCursor.getInt( 0 ); |
023 |
uri = Uri.parse( "content://com.android.contacts/contacts/" + id + "/data" ); |
024 |
Cursor dataCursor = resolver.query(uri, new String[] { "data1" , "mimetype" }, null , null , null ); |
025 |
StringBuilder sb = new StringBuilder(); |
026 |
sb.append( "id=" + id); |
028 |
while (dataCursor.moveToNext()) { |
029 |
String data = dataCursor.getString( 0 ); |
030 |
String type = dataCursor.getString( 1 ); |
031 |
if ( "vnd.android.cursor.item/name" .equals(type)) |
032 |
sb.append( ", name=" + data); |
033 |
else if ( "vnd.android.cursor.item/phone_v2" .equals(type)) |
034 |
sb.append( ", phone=" + data); |
035 |
else if ( "vnd.android.cursor.item/email_v2" .equals(type)) |
036 |
sb.append( ", email=" + data); |
038 |
list.add(sb.toString()); |
042 |
private void queryByName() { |
043 |
ContentResolver resolver = this .getContentResolver(); |
044 |
Uri uri = Uri.parse( "content://com.android.contacts/data/phones/filter/1234567" ); |
045 |
Cursor c = resolver.query(uri, new String[] { "display_name" }, null , null , null ); |
046 |
while (c.moveToNext()) { |
047 |
System.out.println(c.getString( 0 )); |
051 |
private void insert() { |
052 |
ContentResolver resolver = this .getContentResolver(); |
053 |
Uri uri = Uri.parse( "content://com.android.contacts/raw_contacts" ); |
054 |
ContentValues values = new ContentValues(); |
056 |
long id = ContentUris.parseId(resolver.insert(uri, values)); |
059 |
uri = Uri.parse( "content://com.android.contacts/data" ); |
060 |
values.put( "raw_contact_id" , id); |
061 |
values.put( "data2" , "zhang xiangdong" ); |
062 |
values.put( "mimetype" , "vnd.android.cursor.item/name" ); |
063 |
resolver.insert(uri, values); |
067 |
values.put( "raw_contact_id" , id); |
068 |
values.put( "data1" , "1392315698" ); |
069 |
values.put( "data2" , "2" ); |
070 |
values.put( "mimetype" , "vnd.android.cursor.item/phone_v2" ); |
071 |
resolver.insert(uri, values); |
075 |
values.put( "raw_contact_id" , id); |
076 |
values.put( "data1" , "345672@qq.com" ); |
077 |
values.put( "data2" , "1" ); |
078 |
values.put( "mimetype" , "vnd.android.cursor.item/email_v2" ); |
079 |
resolver.insert(uri, values); |
082 |
private void insertWithBatch() throws Exception { |
083 |
ContentResolver resolver = this .getContentResolver(); |
085 |
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
088 |
ContentProviderOperation operation1 = ContentProviderOperation |
089 |
.newInsert(Uri.parse( "content://com.android.contacts/raw_contacts" )) |
090 |
.withValue( "_id" , null ) |
092 |
operations.add(operation1); |
094 |
ContentProviderOperation operation2 = ContentProviderOperation |
095 |
.newInsert(Uri.parse( "content://com.android.contacts/data" )) |
096 |
.withValueBackReference( "raw_contact_id" , 0 ) |
097 |
.withValue( "data2" , "JOhn" ) |
098 |
.withValue( "mimetype" , "vnd.android.cursor.item/name" ) |
100 |
operations.add(operation2); |
102 |
ContentProviderOperation operation3 = ContentProviderOperation |
103 |
.newInsert(Uri.parse( "content://com.android.contacts/data" )) |
104 |
.withValueBackReference( "raw_contact_id" , 0 ) |
105 |
.withValue( "data1" , "135124689745" ) |
106 |
.withValue( "data2" , "2" ) |
107 |
.withValue( "mimetype" , "vnd.android.cursor.item/phone_v2" ) |
109 |
operations.add(operation3); |
111 |
ContentProviderOperation operation4 = ContentProviderOperation |
112 |
.newInsert(Uri.parse( "content://com.android.contacts/data" )) |
113 |
.withValueBackReference( "raw_contact_id" , 0 ) |
114 |
.withValue( "data1" , "123568974@qq.com" ) |
115 |
.withValue( "data2" , "2" ) |
116 |
.withValue( "mimetype" , "vnd.android.cursor.item/email_v2" ) |
118 |
operations.add(operation4); |
121 |
resolver.applyBatch( "com.android.contacts" , operations); |
布局文件: 1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < TextView xmlns:android = "http://schemas.android.com/apk/res/android" |
3 |
android:layout_width = "fill_parent" |
4 |
android:layout_height = "wrap_content" |
5 |
android:text = "@string/hello" |
描述文件: 查看源码 打印 ?
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < manifest xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
package = "com.test.contacts" |
04 |
android:versionCode = "1" |
05 |
android:versionName = "1.0" > |
06 |
< uses-sdk android:minSdkVersion = "10" /> |
07 |
< strong >< uses-permission android:name = "android.permission.READ_CONTACTS" ></ uses-permission > |
08 |
< uses-permission android:name = "android.permission.WRITE_CONTACTS" ></ uses-permission ></ strong > |
09 |
< application android:icon = "@drawable/icon" android:label = "@string/app_name" > |
10 |
< activity android:name = ".MainActivity" |
11 |
android:label = "@string/app_name" > |
13 |
< action android:name = "android.intent.action.MAIN" /> |
14 |
< category android:name = "android.intent.category.LAUNCHER" /> |
运行结果:
- [Android(安卓)Studio导入第三方类库方法] Error:(19, 23) 错误:
- android 存储联系人(save contact) 总结
- 整理出15个Android很有用的代码片段
- android 之 菜单
- android 让5556和5554发发sms
- TextView添加ClickableSpan和LinkMovementMethod之间的关系
- Android给第三方应用添加系统签名的两种方式
- Android(安卓)NDK开发
- Android(安卓)ORM 框架——GreenDao的基本用法
随机推荐
-
2021-03-16:手写代码:单链表归并排序。
-
rpm包管理 yum包管理与编译安装
-
Spark源码系列之Standalone模式下Spark应
-
大数据基础系列之kafka011生产者缓存超时
-
大数据基础系列之kafka知识点和优点
-
大数据基础系列之提交spark应用及依赖管
-
操作系统底层技术——CPU亲和性
-
HDFS学习:HDFS机架感知与副本放置策略
-
SQL on Hadoop 技术分析(二)
-
Yarn【label-based scheduling】实战总结