>>内容提供者
一。数据库操作器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// 这是一个新创建的类 public class PersonSqliteOpenHelper extends SQLiteOpenHelper { public PersonSqliteOpenHelper(Context context) { // 上下文,数据库文件,工厂,版本 super(context, "SqliteDB.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { String sql = "create table person(id integer primary key, name varchar(20), age integer);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(oldVersion ==1 && newVersion ==2){ db.execSQL("alter table person add balance integer;"); System.err.println("升级啦----------------------------"); } } } |
二。创建内容提供者:
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 |
// 新创建的类 public class MyProvider extends ContentProvider { private static final String AUTHORITY = "com.cuiweiyou.sqlitetest.provider.MyProvider"; // 对外提供的总机号,第三方拨打此号码 private static final int PRESON_INSERT_CODE = 0; // 操作person表添加的操作的uri匹配码。分机号 private static final int PERSON_DELETE_CODE = 1; private static final int PERSON_UPDATE_CODE = 2; private static final int PERSON_QUERY_ALL_CODE = 3; private static final int PERSON_QUERY_ITEM_CODE = 4; private static UriMatcher uriMatcher; // uri匹配器 private PersonSqliteOpenHelper mOpenHelper; // person表的数据库帮助对象 static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); // 初始.无匹配 // 添加一些uri(分机号) // content://com.cuiweiyou.sqlitetest.provider.MyProvider/person/insert uriMatcher.addURI(AUTHORITY, "person/insert", PRESON_INSERT_CODE); // 0 // content://com.cuiweiyou.sqlitetest.provider.MyProvider/person/delete uriMatcher.addURI(AUTHORITY, "person/delete", PERSON_DELETE_CODE); // 1 // content://com.cuiweiyou.sqlitetest.provider.MyProvider/person/update uriMatcher.addURI(AUTHORITY, "person/update", PERSON_UPDATE_CODE); // 2 // content://com.cuiweiyou.sqlitetest.provider.MyProvider/person/queryAll uriMatcher.addURI(AUTHORITY, "person/queryAll", PERSON_QUERY_ALL_CODE); // 3 // content://com.cuiweiyou.sqlitetest.provider.MyProvider/person/query/# uriMatcher.addURI(AUTHORITY, "person/query/#", PERSON_QUERY_ITEM_CODE); // 4 } @Override public boolean onCreate() { mOpenHelper = new PersonSqliteOpenHelper(getContext()); return true; } @Override public Uri insert(Uri uri, ContentValues values) { switch (uriMatcher.match(uri)) { case PRESON_INSERT_CODE: // 添加人到person表中 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { long id = db.insert("person", null, values); db.close(); return ContentUris.withAppendedId(uri, id); } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { switch (uriMatcher.match(uri)) { case PERSON_DELETE_CODE: // 在person表中删除数据的操作 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { int count = db.delete("person", selection, selectionArgs); db.close(); return count; } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { switch (uriMatcher.match(uri)) { case PERSON_UPDATE_CODE: // 更新person表的操作 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { int count = db.update("person", values, selection, selectionArgs); db.close(); return count; } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return 0; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); switch (uriMatcher.match(uri)) { case PERSON_QUERY_ALL_CODE: // 查询所有人的uri if(db.isOpen()) { Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor; // db.close(); 返回cursor结果集时, 不可以关闭数据库 } break; case PERSON_QUERY_ITEM_CODE: // 查询的是单条数据, uri末尾出有一个id if(db.isOpen()) { long id = ContentUris.parseId(uri); Cursor cursor = db.query("person", projection, "_id = ?", new String[]{id + ""}, null, null, sortOrder); return cursor; } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return null; } @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)) { case PERSON_QUERY_ALL_CODE: // 返回多条的MIME-type return "vnd.android.cursor.dir/person"; case PERSON_QUERY_ITEM_CODE: // 返回单条的MIME-TYPE return "vnd.android.cursor.item/person"; default: break; } return null; } } |
三。注册内容提供者:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <manifest... > <uses-sdk.../> <application...> <activity...> <intent-filter>...</intent-filter> </activity> <!-- name="内容提供者类" authorities="自定义的对外主机号" --> <provider android:name="com.cuiweiyou.sqlitetest.provider.MyProvider" android:authorities="com.cuiweiyou.sqlitetest.provider.MyProvider"/> </application> </manifest> |
>>内容观察者
一。创建内容观察者:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// 这是程序入口类 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 解析器 ContentResolver resolver = getContentResolver(); // 监视自己的 对外公开的总机 Uri uri = Uri.parse("content://com.cuiweiyou.sqlitetest.provider.MyProvider"); // 一旦发生变化,就是有其他人使用了 resolver.registerContentObserver(Uri.parse("content://sms/"), true, new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { System.err.println("警示:有第三方调用"); } }); } } |
二。分派观察者:
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 |
public class MyProvider extends ContentProvider { private static final String AUTHORITY = "com.cuiweiyou.sqlitetest.provider.MyProvider"; // 对外提供的总机号,第三方拨打此号码 private ... static { ... } @Override public boolean onCreate() { ... } @Override public Uri insert(Uri uri, ContentValues values) { switch (uriMatcher.match(uri)) { case PRESON_INSERT_CODE: // 添加人到person表中 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { long id = db.insert("person", null, values); db.close(); // 当此分机号的功能被第三方调用时,即向监听器发送通知(监听目标,监听器)。监听器的onChange方法会响应 // 发送到自身的监听器 getContext().getContentResolver().notifyChange(Uri.parse(AUTHORITY), null); return ContentUris.withAppendedId(uri, id); } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { ... getContext().getContentResolver().notifyChange(Uri.parse(AUTHORITY), null); return count; } ... } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { ... getContext().getContentResolver().notifyChange(Uri.parse(AUTHORITY), null); return count; } ... } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { ... } @Override public String getType(Uri uri) { ... } } |
>>第三方内容使用者
一。JUnit测试注册:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <manifest... > <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.cuiweiyou.myprovider"/> <uses-sdk.../> <application...> <uses-library android:name="android.test.runner"/> <activity...> <intent-filter>...</intent-filter> </activity> </application> </manifest> |
二。测试类:
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 |
public class ProviderTest extends AndroidTestCase { private String TAG = "TextCase"; // 内容提供者的电话总机 private String uriProvider = "content://com.cuiweiyou.sqlitetest.provider.MyProvider"; public void testInsert() { Uri uri = Uri.parse(uriProvider + "/person/insert"); // 内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); ContentValues values = new ContentValues(); values.put("name", "哈拉哈拉"); values.put("age", 30); uri = resolver.insert(uri, values); Log.i(TAG, "uri: " + uri); long id = ContentUris.parseId(uri); Log.i(TAG, "添加到: " + id); } public void testDelete() { Uri uri = Uri.parse(uriProvider + "/person/delete"); // 内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); String where = "_id = ?"; String[] selectionArgs = {"21"}; int count = resolver.delete(uri, where, selectionArgs); Log.i(TAG, "删除行: " + count); } public void testUpdate() { Uri uri = Uri.parse(uriProvider + "/person/update"); // 内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); ContentValues values = new ContentValues(); values.put("name", "lisi"); int count = resolver.update(uri, values, "_id = ?", new String[]{"20"}); Log.i(TAG, "更新行: " + count); } public void testQueryAll() { Uri uri = Uri.parse(uriProvider + "/person/queryAll"); // 内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, "_id desc"); if(cursor != null && cursor.getCount() > 0) { int id; String name; int age; while(cursor.moveToNext()) { id = cursor.getInt(0); name = cursor.getString(1); age = cursor.getInt(2); Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age); } cursor.close(); } } public void testQuerySingleItem() { Uri uri = Uri.parse(uriProvider + "/person/query/#"); // 在uri的末尾添加一个id content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/query/20 uri = ContentUris.withAppendedId(uri, 20); // 内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, null); if(cursor != null && cursor.moveToFirst()) { int id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); cursor.close(); Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age); } } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/151.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设