内容提供者ContentProvider
当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。ContentProvider为存储和获取数据提供了统一的接口
注意: android:exported="true"
这个属性不是限制把服务暴露给其他应用程序的唯一方法。还可以使用权限来限制能够跟该服务交互的外部实体
在需要提供的应用程序AndroidManifest.xml文件章添加如下文件
1
2
3
4
5
6
|
<!-- 配置内容提供者,android:authorities为该内容提供者取名作为在本应用中的唯一标识 --> < provider android:name = "com.example.usersqllite.provider.UserProvider" android:authorities = "com.sanya.user.provider" android:exported = "true" > </ provider > |
内容提供者需要的代码:
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
|
package com.example.usersqllite.provider; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import com.example.usersqllite.domain.UserSqlLiteHelp; public class UserProvider extends ContentProvider { private static final int QUERY = 1 ; private static final int INSERT = 2 ; private static final int DELETE = 3 ; private static final int UPDATE = 4 ; private SQLiteDatabase database; // 创建uri匹配器 private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); static { // 建立 匹配规则 matcher.addURI( "com.sanya.user.provider" , "query" , QUERY); matcher.addURI( "com.sanya.user.provider" , "insert" , INSERT); matcher.addURI( "com.sanya.user.provider" , "delete" , DELETE); matcher.addURI( "com.sanya.user.provider" , "update" , UPDATE); } @Override public boolean onCreate() { UserSqlLiteHelp help = new UserSqlLiteHelp(getContext()); database = help.getWritableDatabase(); return true ; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { int match = matcher.match(uri); if (match == QUERY) { // 查询处理 UserSqlLiteHelp userSqlLiteHelp = new UserSqlLiteHelp(getContext()); SQLiteDatabase readableDatabase = userSqlLiteHelp .getReadableDatabase(); return readableDatabase.query( "user" , new String[] { "id" , "name" , "age" }, null , null , null , null , null ); } else { throw new RuntimeException( "查询出错,路径不能被识别" ); } } @Override public String getType(Uri uri) { return null ; } @Override public Uri insert(Uri uri, ContentValues values) { int match = matcher.match(uri); if (match == INSERT) { database.insert( "user" , null , values); } else { throw new RuntimeException( "插入数据出错,路径不能被识别" ); } return null ; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int match = matcher.match(uri); if (match == DELETE) { database.delete( "user" , selection, selectionArgs); } else { throw new RuntimeException( "删除数据出错,路径不能被识别" ); } return 0 ; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int match = matcher.match(uri); if (match == UPDATE) { database.update( "user" , values, selection, selectionArgs); } else { throw new RuntimeException( "更新数据出错,路径不能被识别" ); } return 0 ; } } |
其他应用程序调用的测试代码:
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
|
public void query(View view) { ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(uri, null , null , null , null ); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex( "id" )); String name = cursor.getString(cursor.getColumnIndex( "name" )); int age = cursor.getInt(cursor.getColumnIndex( "age" )); System.out.println(id + "--" + name + "---" + age); } cursor.close(); } public void delete(View view) { ContentResolver resolver = getContentResolver(); resolver.delete(uri, "name=?" , new String[] { "哈哈" }); } public void update(View view) { ContentResolver resolver = getContentResolver(); ContentValues values = new ContentValues(); values.put( "name" , "嘿嘿" ); values.put( "age" , 19 ); resolver.update(uri, values, "name=?" , new String[] { "三亚" }); } public void insert(View view) { ContentResolver resolver = getContentResolver(); ContentValues values = new ContentValues(); values.put( "name" , "大哥" ); values.put( "age" , 100 ); resolver.insert(uri, values); } |