本文實例講述了android內容提供者contentprovider用法。分享給大家供大家參考,具體如下:
personcontentprovider內容提供者類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
package com.ljq.db; import android.content.contentprovider; import android.content.contenturis; import android.content.contentvalues; import android.content.urimatcher; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.net.uri; import android.text.textutils; /** * 內容提供者 * * 作用:統一數據訪問方式,方便外部調用 * * @author jiqinlin * */ public class personcontentprovider extends contentprovider { // 數據集的mime類型字符串則應該以vnd.android.cursor.dir/開頭 public static final string persons_type = "vnd.android.cursor.dir/person" ; // 單一數據的mime類型字符串應該以vnd.android.cursor.item/開頭 public static final string persons_item_type = "vnd.android.cursor.item/person" ; public static final string authority = "com.ljq.provider.personprovider" ; // 主機名 /* 自定義匹配碼 */ public static final int persons = 1; /* 自定義匹配碼 */ public static final int person = 2 ; public static final uri persons_uri = uri.parse( "content://" + authority + "/person" ); private dbopenhelper dbopenhelper = null ; // urimatcher類用來匹配uri,使用match()方法匹配路徑時返回匹配碼 private static final urimatcher urimatcher; static { // 常量urimatcher.no_match表示不匹配任何路徑的返回碼 urimatcher = new urimatcher(urimatcher.no_match); // 如果match()方法匹配content://com.ljq.provider.personprovider/person路徑,返回匹配碼為persons urimatcher.adduri(authority, "person" , persons); // 如果match()方法匹配content://com.ljq.provider.personprovider/person/230路徑,返回匹配碼為person urimatcher.adduri(authority, "person/#" , person); } @override public boolean oncreate() { dbopenhelper = new dbopenhelper( this .getcontext()); return true ; } @override public uri insert(uri uri, contentvalues values){ sqlitedatabase db = dbopenhelper.getwritabledatabase(); long id = 0 ; switch (urimatcher.match(uri)) { case persons: id = db.insert( "person" , "name" , values); // 返回的是記錄的行號,主鍵為int,實際上就是主鍵值 return contenturis.withappendedid(uri, id); case person: id = db.insert( "person" , "name" , values); string path = uri.tostring(); return uri.parse(path.substring( 0 , path.lastindexof( "/" ))+id); // 替換掉id default : throw new illegalargumentexception( "unknown uri " + uri); } } @override public int delete(uri uri, string selection, string[] selectionargs) { sqlitedatabase db = dbopenhelper.getwritabledatabase(); int count = 0 ; switch (urimatcher.match(uri)) { case persons: count = db.delete( "person" , selection, selectionargs); break ; case person: // 下面的方法用于從uri中解析出id,對這樣的路徑content://com.ljq.provider.personprovider/person/10 // 進行解析,返回值為10 long personid = contenturis.parseid(uri); string where = "id=" + personid; // 刪除指定id的記錄 where += !textutils.isempty(selection) ? " and (" + selection + ")" : "" ; // 把其它條件附加上 count = db.delete( "person" , where, selectionargs); break ; default : throw new illegalargumentexception( "unknown uri " + uri); } db.close(); return count; } @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { sqlitedatabase db = dbopenhelper.getwritabledatabase(); int count = 0 ; switch (urimatcher.match(uri)) { case persons: count = db.update( "person" , values, selection, selectionargs); break ; case person: // 下面的方法用于從uri中解析出id,對這樣的路徑content://com.ljq.provider.personprovider/person/10 // 進行解析,返回值為10 long personid = contenturis.parseid(uri); string where = "id=" + personid; // 獲取指定id的記錄 where += !textutils.isempty(selection) ? " and (" + selection + ")" : "" ; // 把其它條件附加上 count = db.update( "person" , values, where, selectionargs); break ; default : throw new illegalargumentexception( "unknown uri " + uri); } db.close(); return count; } @override public string gettype(uri uri) { switch (urimatcher.match(uri)) { case persons: return persons_type; case person: return persons_item_type; default : throw new illegalargumentexception( "unknown uri " + uri); } } @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitedatabase db = dbopenhelper.getreadabledatabase(); switch (urimatcher.match(uri)) { case persons: return db.query( "person" , projection, selection, selectionargs, null , null , sortorder); case person: // 下面的方法用于從uri中解析出id,對這樣的路徑content://com.ljq.provider.personprovider/person/10 // 進行解析,返回值為10 long personid = contenturis.parseid(uri); string where = "id=" + personid; // 獲取指定id的記錄 where += !textutils.isempty(selection) ? " and (" + selection + ")" : "" ; // 把其它條件附加上 return db.query( "person" , projection, where, selectionargs, null , null , sortorder); default : throw new illegalargumentexception( "unknown uri " + uri); } } } |
文件清單
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.ljq.sql" android:versioncode= "1" android:versionname= "1.0" > <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <uses-library android:name= "android.test.runner" /> <activity android:name= ".sqlactivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action.main" /> <category android:name= "android.intent.category.launcher" /> </intent-filter> </activity> <provider android:name= "com.ljq.db.personcontentprovider" android:authorities= "com.ljq.provider.personprovider" /> </application> <uses-sdk android:minsdkversion= "7" /> <instrumentation android:name= "android.test.instrumentationtestrunner" android:targetpackage= "com.ljq.sql" android:label= "tests for my app" /> </manifest> |
personcontentprovidertest內容提供者測試類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.ljq.test; import android.content.contentresolver; import android.content.contentvalues; import android.database.cursor; import android.net.uri; import android.test.androidtestcase; import android.util.log; /** * 外部訪問內容提供者 * * @author jiqinlin * */ public class personcontentprovidertest extends androidtestcase{ private static final string tag = "personcontentprovidertest" ; public void testsave() throws throwable{ contentresolver contentresolver = this .getcontext().getcontentresolver(); uri inserturi = uri.parse( "content://com.ljq.provider.personprovider/person" ); contentvalues values = new contentvalues(); values.put( "name" , "ljq" ); values.put( "phone" , "1350000009" ); uri uri = contentresolver.insert(inserturi, values); log.i(tag, uri.tostring()); } public void testupdate() throws throwable{ contentresolver contentresolver = this .getcontext().getcontentresolver(); uri updateuri = uri.parse( "content://com.ljq.provider.personprovider/person/1" ); contentvalues values = new contentvalues(); values.put( "name" , "linjiqin" ); contentresolver.update(updateuri, values, null , null ); } public void testfind() throws throwable{ contentresolver contentresolver = this .getcontext().getcontentresolver(); //uri uri = uri.parse("content://com.ljq.provider.personprovider/person"); uri uri = uri.parse( "content://com.ljq.provider.personprovider/person" ); cursor cursor = contentresolver.query(uri, null , null , null , "id asc" ); while (cursor.movetonext()){ int personid = cursor.getint(cursor.getcolumnindex( "id" )); string name = cursor.getstring(cursor.getcolumnindex( "name" )); string phone = cursor.getstring(cursor.getcolumnindex( "phone" )); log.i(tag, "personid=" + personid + ",name=" + name+ ",phone=" + phone); } cursor.close(); } public void testdelete() throws throwable{ contentresolver contentresolver = this .getcontext().getcontentresolver(); uri uri = uri.parse( "content://com.ljq.provider.personprovider/person/1" ); contentresolver.delete(uri, null , null ); } } |
希望本文所述對大家android程序設計有所幫助。