代码改变世界

ContentProvider和ContentResolver应用

2012-04-27 17:59  ...平..淡...  阅读(474)  评论(0编辑  收藏  举报

主要是调用现成的ContentProvider...

ContentResolver是通过URI来查询ContentProvider中提供的数据.
在android中,每个应用程序是可以实现数据共享的,都拥有一个contentprovider实例,而contentresolver用于管理程序的contentprovider实例.
首先需要获得一个ContentResolver的实例,可通过Activity的成员方法getContentResovler()方法:ContentResolver contentResolver = getContentResolver();

ContentResolver实例带的方法可实现找到指定的Contentprovider并获取到Contentprovider的数据。

When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.


在配置文件中要加上权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>



step1:建立android工程,命名为ContentProviderTest.
step2:设置配置文件
main.xml
main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/hello" />
11 
12     <Button
13         android:id="@+id/button01"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content"
16         android:text="@string/read" />
17 
18 </LinearLayout>

strings.xml
strings.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 
4     <string name="hello">Hello World, ContentProviderTest!</string>
5     <string name="app_name">ContentProviderTest</string>
6     <string name="read">读取联系人</string>
7 </resources>

AndroidManifest.xml
AndroidManifest.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.cb.contentprovider"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk android:minSdkVersion="10" />
 8 
 9     <application
10         android:icon="@drawable/ic_launcher"
11         android:label="@string/app_name" >
12         <activity
13             android:label="@string/app_name"
14             android:name=".ContentProviderTest" >
15             <intent-filter >
16                 <action android:name="android.intent.action.MAIN" />
17 
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21     </application>
22 
23     <!-- 必须加入访问权限,官方文档中解释:Allows an application to read the user's contacts data. -->
24     <uses-permission android:name="android.permission.READ_CONTACTS" />
25 
26 </manifest>


step3:ContentProviderTest.java
ContentProviderTest.java
 1 package com.cb.contentprovider;
 2 
 3 import android.app.Activity;
 4 import android.content.ContentResolver;
 5 import android.database.Cursor;
 6 import android.os.Bundle;
 7 import android.provider.ContactsContract;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.Toast;
11 
12 //使用现成的Content Provider
13 
14 public class ContentProviderTest extends Activity {
15     private Button mButton;
16 
17     @Override
18     public void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.main);
21 
22         mButton = (Button) findViewById(R.id.button01);
23 
24         mButton.setOnClickListener(new Button.OnClickListener() {
25 
26             @Override
27             public void onClick(View v) {
28 
29                 /*
30                  * 官方文档对ContentReceiver的解释: This class provides applications
31                  * access to the content model
32                  */
33                 ContentResolver mContentResolver = getContentResolver();
34 
35                 // 读取所有联系人记录
36                 Cursor cursor = mContentResolver.query(
37                         ContactsContract.Contacts.CONTENT_URI, null, null,
38                         null, null);
39                 if (cursor.moveToFirst()) { // 当记录不为空时
40 
41                     /*
42                      * 根据getColumnIndex(string name)中的
43                      * 参数name的名称获得它的列索引,
44                      * 因为getString,getInt这些方法需要的是列索引,而不是列名称。!!!!!提醒自己
45                      */
46                     int idColumn = cursor
47                             .getColumnIndex(ContactsContract.Contacts._ID);
48                     int nameColumn = cursor
49                             .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
50                     do {
51                         // 获得联系人的id号
52                         String contactId = cursor.getString(idColumn);
53                         // 获得联系人的姓名
54                         String contactName = cursor.getString(nameColumn);
55 
56                         Toast.makeText(ContentProviderTest.this,
57                                 "id号:" + contactId + ", 联系人姓名:" + contactName,
58                                 Toast.LENGTH_SHORT).show();
59 
60                         // 查看该联系人有多少个电话号码
61                         int phoneCount = cursor.getInt(cursor
62                                 .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
63                         if (phoneCount > 0) {
64                             // 获得联系人的电话号码列表
65                             Cursor phonesCursor = mContentResolver
66                                     .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
67                                             null,
68                                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID
69                                                     + " = " + contactId, null,
70                                             null);
71                             if (phonesCursor.moveToFirst()) {
72                                 do {
73                                     // 遍历所有的电话号码
74                                     String phoneNumber = phonesCursor.getString(phonesCursor
75                                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
76                                     Toast.makeText(ContentProviderTest.this,
77                                             "联系人电话:" + phoneNumber,
78                                             Toast.LENGTH_LONG).show();
79                                 } while (phonesCursor.moveToNext());
80                             }
81 
82                         }
83 
84                     } while (cursor.moveToNext());
85                 }
86             }
87         });
88 
89     }
90 }

 
step4:图示