您的位置:首页 > 移动开发 > Android开发

Android组件系列----ContentProvider内容提供者【4】

2017-05-01 18:00 661 查看
(4)单元測试类:

这里须要涉及到另外一个知识:ContentResolver内容訪问者。

要想訪问ContentProvider。则必须使用ContentResolver。

能够通过ContentResolver来操作ContentProvider所暴露处理的接口。一般使用Content.getContentResolver()方法获取ContentResolver对象。第一段中已经提到:ContentProvider有非常多对外能够訪问的方法,在ContentResolver中均有同名的方法,是一一相应的。

所以它也存在insert、query、update、delete等方法。于是单元測试类能够这样写:(注:单元測试假设不清楚。能够參考另外一篇文章: JUnit单元測试的使用

MyTest.java:



1 package com.example.contentresolvertest;
2
3 import android.content.ContentResolver;
4 import android.content.ContentValues;
5 import android.database.Cursor;
6 import android.net.Uri;
7 import android.os.Bundle;
8 import android.test.AndroidTestCase;
9 import android.util.Log;
10
11 public class MyTest extends AndroidTestCase {
12
13     public MyTest() {
14         // TODO Auto-generated constructor stub
15
16     }
17
18     public void calltest() {
19         ContentResolver contentResolver = getContext().getContentResolver();
20         Uri uri = Uri
21                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person");
22         Bundle bundle = contentResolver.call(uri, "method", null, null);
23         String returnCall = bundle.getString("returnCall");
24         Log.i("main", "-------------->" + returnCall);
25     }
26
27     //測试方法:向数据库中加入记录。假设之前没有数据库,则会自己主动创建
28     public void insert() {
29         // 使用内容解析者ContentResolver訪问内容提供者ContentProvider
30         ContentResolver contentResolver = getContext().getContentResolver();
31         ContentValues values = new ContentValues();
32         values.put("name", "生命贰号");
33         values.put("address", "湖北");
34         // content://authorities/person
35         // http:// 36         Uri uri = Uri
37                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person");
38         contentResolver.insert(uri, values);
39     }
40
41     //測试方法:删除单条记录。假设要删除全部记录:content://com.example.contentprovidertest01.PersonContentProvider/person
42     public void delete() {
43         ContentResolver contentResolver = getContext().getContentResolver();
44         Uri uri = Uri
45                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person/2");//删除id为1的记录
46         contentResolver.delete(uri, null, null);
47     }
48
49     //測试方法:依据条件删除记录。
50     public void deletes() {
51         ContentResolver contentResolver = getContext().getContentResolver();
52         Uri uri = Uri
53                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person");
54         String where = "address=?";
55         String[] where_args = { "HK" };
56         contentResolver.delete(uri, where, where_args);  //第二个參数表示查询的条件"address=?

",第三个參数表示占位符中的详细内容
57     }
58
59     //方法:依据id改动记录。注:非常少有批量改动的情况。
60     public void update() {
61         ContentResolver contentResolver = getContext().getContentResolver();
62         Uri uri = Uri
63                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person/2");
64         ContentValues values = new ContentValues();
65         values.put("name", "李四");
66         values.put("address", "上海");
67         contentResolver.update(uri, values, null, null);
68     }
69
70     //方法:依据条件来改动记录。
71     public void updates() {
72         ContentResolver contentResolver = getContext().getContentResolver();
73         Uri uri = Uri
74                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person/student");
75         ContentValues values = new ContentValues();
76         values.put("name", "王五");
77         values.put("address", "深圳");
78         String where = "address=?";
79         String[] where_args = { "beijing" };
80         contentResolver.update(uri, values, where, where_args);
81     }
82
83     //測试方法:查询全部记录。假设要查询单条记录:content://com.example.contentprovidertest01.PersonContentProvider/person/1
84     public void query() {
85         ContentResolver contentResolver = getContext().getContentResolver();
86         Uri uri = Uri
87                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person");
88         Cursor cursor = contentResolver.query(uri, null, null, null, null);
89         while (cursor.moveToNext()) {
90             Log.i("MyTest",
91                     "--->>"
92                             + cursor.getString(cursor.getColumnIndex("name")));
93         }
94     }
95
96     //測试方法:依据条件查询全部记录。
97     public void querys() {
98         ContentResolver contentResolver = getContext().getContentResolver();
99         Uri uri = Uri
100                 .parse("content://com.example.contentprovidertest01.PersonContentProvider/person");
101         String where = "address=?";
102         String[] where_args = { "深圳" };
103         Cursor cursor = contentResolver.query(uri, null, where, where_args,
104                 null);
105         while (cursor.moveToNext()) {
106             Log.i("main",
107                     "-------------->"
108                             + cursor.getString(cursor.getColumnIndex("name")));
109         }
110     }
111
112 }




既然ContetProvider实现的是跨应用訪问数据,那这个測试类Test.java就应该写在还有一个应用程序中才行。于是,我们新建另外一个project文件ContentResolverTest,在里面加入单元測试,里面的代码事实上和上方的Test.java的代码是一模一样的。

执行单元測试。依旧能在ContentResolverTest中实现对ContentProviderTest01中的CRUD.核心在于:使用应用1中的内容解析者ContentResolver訪问应用2中的内容提供者ContentProvider

如今执行ContentProviderTest01中的单元測试类:

1、执行insert()方法,实现插入操作。后台打印例如以下:





上图中红框部分表明,这个uri就是代表内容提供者中,person表中。id为1的数据。

此时,打开file Explorer,进行查看,发现确实多了个文件:





注意:假设SQLite中之前没有mydb.db这个数据库。当实现插入操作时,会自己主动创建mydb.db这个数据库,并自己主动创建person表(由于在PersonDao类中运行了getWritableDatabase()方法)。

如今将上图中的mydb.db导出。然后用SQLiteExpert软件打开,输入sql查询语句。就能够看到person表中的数据了:





假设再运行insert()方法,又会继续加入一条记录(id是自己主动增长的)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: