第67章、使用ContentProvider操作联系人(从零开始学Android)
来源:互联网 发布:笔记本硬盘的数据导出 编辑:程序博客网 时间:2024/06/05 11:51
你知道吗?我们天天在手机上使用的联系人,其本质在Android中也是保存在一个SQLite数据库中。
它的路径为:/data/data/com.android.providers.contacts/databases/contacts2.db
android也提供了很多接口,通过ContentResolver().query方法,传入不同的URI即可访问相应的数据集。在联系人数据库里面联系人和电话号码是分别存在两个表里面的,因为存在一个联系人拥有几个号码的情况,所以android为联系人和手机号码分别单独创建了相应的视图。联系人信息的视图里面只保存与联系人相关的资料,例如姓名,是否有手机号码等。而手机号码资料则是每一个电话号码为一条记录,如果有一个联系人有3个号码,则里面会出现3个该联系人的记录,号码分别为他的三个号码。
如果是需要读取联系人信息,传入的URI为:ContactsContract.Contacts.CONTENT_URI
如果是需要读取手机号码信息传入的URI为:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
一、设计界面
1、布局文件
打开res/layout/activity_main.xml文件。
输入以下代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="联系人列表" /> <ListView android:id="@+id/contactlist" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取联系人信息" /></LinearLayout>
2、自定义ListView文件
打开res/layout/contacts.xml文件。
输入以下代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/_id" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
二、程序文件
打开“src/com.genwoxue.contentprovider_a/MainActivity.java”文件。
然后输入以下代码:
package com.genwoxue.contentprovider_a;import android.os.Bundle;import android.provider.ContactsContract;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleCursorAdapter;import android.app.Activity;import android.database.Cursor;public class MainActivity extends Activity {private ListView lvContacts=null;private Cursor cursor=null;private Button btnRead=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lvContacts=(ListView)super.findViewById(R.id.contactlist);btnRead=(Button)super.findViewById(R.id.read);btnRead.setOnClickListener(new OnClickListener(){ public void onClick(View v) { cursor=MainActivity.this.getContentResolver() .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); String[] from={ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME}; int to[]={R.id._id,R.id.name}; SimpleCursorAdapter adapter = new SimpleCursorAdapter( MainActivity.this, R.layout.contacts, cursor, from, to); lvContacts.setAdapter(adapter); }});}}
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.genwoxue.contentprovider_a" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.READ_CONTACTS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.genwoxue.contentprovider_a.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>
注意:由于要进行读写外部存储卡操作,故而需要在AndroidManifest.xml文件中添加两项权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
四、运行结果
- 第67章、使用ContentProvider操作联系人(从零开始学Android)
- 第68章、使用ContentProvider操作通话记录(从零开始学Android)
- Android--操作联系人的ContentProvider
- Android 使用ContentProvider 添加联系人
- 第61章、资源文件之存取操作(从零开始学Android)
- 第62章、XML文件-DOM操作(从零开始学Android)
- 第63章、XML文件-SAX操作(从零开始学Android)
- 第4章、打包Android应用(从零开始学Android)
- 第99章、Android调用Javascript(从零开始学Android)
- 第104章、Android访问WebService(从零开始学Android)
- 第4章、打包Android应用(从零开始学Android)
- 第4章、打包Android应用(从零开始学Android)
- 第4章、打包Android应用(从零开始学Android)
- 第99章、Android调用Javascript(从零开始学Android)
- 第66章、使用SQLite本地数据库(从零开始学Android)
- Android通讯录(联系人)-ContentProvider
- 第9章、图像按钮ImageButton(从零开始学Android)
- 第10章、图像ImageView(从零开始学Android)
- 浅析count(1) count(*) count(col)的区别和性能
- tar
- 解决 firefox 不支持 document.all的方法
- 2013年专利代理人考试复习规划
- c++ 覆盖、重载、隐藏
- 第67章、使用ContentProvider操作联系人(从零开始学Android)
- LIBCMTD.lib与libcpmtd冲突的解决方法。
- Android中Bitmap,byte[],Drawable相互转化
- Android Dialog 多次启动的问题
- 带有指针参数的函数内给指针参数赋值时注意事项
- tts语音播报
- K3 增加关系字段SQL
- 第68章、使用ContentProvider操作通话记录(从零开始学Android)
- 观人有四招(转)