歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之ContentProvider

Android應用開發之ContentProvider

日期:2017/3/1 10:44:31   编辑:Linux編程

1、PersonProvider

  1. package cn.class3g.db;
  2. import cn.class3g.service.DatabaseHelper;
  3. import Android.content.ContentProvider;
  4. import android.content.ContentUris;
  5. import android.content.ContentValues;
  6. import android.content.UriMatcher;
  7. import android.database.Cursor;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.net.Uri;
  10. public class PersonProvider extends ContentProvider {
  11. private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
  12. private static final int PERSONS = 1;
  13. private static final int PERSON = 2;
  14. private DatabaseHelper dbHelper;
  15. static {
  16. matcher.addURI("cn.class3g.providers.personprovider", "person", PERSONS);
  17. matcher.addURI("cn.class3g.providers.personprovider", "person/#",
  18. PERSON);
  19. }
  20. public boolean onCreate() {
  21. dbHelper = new DatabaseHelper(this.getContext());
  22. return true;
  23. }
  24. // content://cn.itcast.provides.personprovider/person
  25. public Uri insert(Uri uri, ContentValues values) {
  26. SQLiteDatabase db = dbHelper.getWritableDatabase();
  27. long rowId;
  28. switch (matcher.match(uri)) {
  29. case PERSONS: //向表中添加新紀錄並返回其行號
  30. rowId = db.insert("person", "personid", values);
  31. return ContentUris.withAppendedId(uri, rowId);
  32. default:
  33. throw new IllegalArgumentException("Unknow Uri:" + uri);
  34. }
  35. }
  36. public Cursor query(Uri uri, String[] projection, String selection,
  37. String[] selectionArgs, String sortOrder) {
  38. SQLiteDatabase db = dbHelper.getReadableDatabase();
  39. switch (matcher.match(uri)) {
  40. case PERSONS:
  41. return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
  42. case PERSON:
  43. long personid = ContentUris.parseId(uri);
  44. String where = "personid="+ personid;
  45. if(selection!=null && !"".equals(selection)){
  46. wherewhere = where + " and "+ selection;
  47. }
  48. return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
  49. default:
  50. throw new IllegalArgumentException("Unknown Uri:"+ uri);
  51. }
  52. }
  53. // content://cn.itcast.provides.personprovider/person 更新表中的所有記錄
  54. // content://cn.itcast.provides.personprovider/person/10 更新表中指定id的記錄
  55. public int update(Uri uri, ContentValues values, String selection,
  56. String[] selectionArgs) {
  57. SQLiteDatabase db = dbHelper.getWritableDatabase();
  58. int num;
  59. switch(matcher.match(uri)){
  60. case PERSONS: //更新指定記錄
  61. num = db.update("person", values, selection, selectionArgs);
  62. break;
  63. case PERSON:
  64. long personid = ContentUris.parseId(uri);
  65. String where = "personid=" + personid;
  66. if(selection != null){
  67. where += " and " + selection;
  68. }
  69. num = db.update("person", values, where, selectionArgs);
  70. break;
  71. default:
  72. throw new IllegalArgumentException("Unknow Uri"+uri);
  73. }
  74. return num;
  75. }
  76. public int delete(Uri uri, String selection, String[] selectionArgs) {
  77. SQLiteDatabase db = dbHelper.getWritableDatabase();
  78. int num = 0;
  79. switch (matcher.match(uri)) {
  80. case PERSONS:
  81. num = db.delete("person", selection, selectionArgs);
  82. break;
  83. case PERSON:
  84. long personid = ContentUris.parseId(uri);
  85. String where = "personid="+ personid;
  86. if(selection!=null && !"".equals(selection)){
  87. wherewhere = where + " and "+ selection;
  88. }
  89. num = db.delete("person", where, selectionArgs);
  90. break;
  91. default:
  92. throw new IllegalArgumentException("Unknown Uri:"+ uri);
  93. }
  94. return num;
  95. }
  96. public String getType(Uri uri) {
  97. switch (matcher.match(uri)) {
  98. case PERSONS:
  99. return "vnd.android.cursor.dir/person";
  100. case PERSON:
  101. return "vnd.android.cursor.item/person";
  102. default:
  103. throw new IllegalArgumentException("Unknown Uri:"+ uri);
  104. }
  105. }
  106. }

2、配置

  1. <provider
  2. android:authorities="cn.class3g.providers.personprovider"
  3. android:name="PersonProvider" >
  4. </provider>

3、建立測試工程編寫測試代碼

  1. package cn.class3g.visitor;
  2. import android.content.ContentResolver;
  3. import android.content.ContentValues;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.test.AndroidTestCase;
  7. import android.util.Log;
  8. public class AccessContentProviderTest extends AndroidTestCase {
  9. public void testSave() throws Throwable{
  10. ContentResolver resolver = this.getContext().getContentResolver();
  11. Uri insertUri = Uri.parse("content://cn.class3g.providers.personprovider/person");
  12. ContentValues values = new ContentValues();
  13. values.put("name", "laozhang");
  14. values.put("age", "50");
  15. Uri uri = resolver.insert(insertUri, values);
  16. Log.i("TAG", uri.toString());
  17. }
  18. public void testQuery() throws Throwable{
  19. ContentResolver resolver = this.getContext().getContentResolver();
  20. Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person");
  21. Cursor cursor = resolver.query(uri, null, null, null, "personid asc");
  22. while(cursor.moveToNext()){
  23. int personid = cursor.getInt(cursor.getColumnIndex("personid"));
  24. String name = cursor.getString(cursor.getColumnIndex("name"));
  25. Log.i("TAG", "personid="+ personid + ",name="+ name);
  26. }
  27. cursor.close();
  28. }
  29. public void testUpdate() throws Throwable{
  30. ContentResolver contentResolver = this.getContext().getContentResolver();
  31. Uri updateUri = Uri.parse("content://cn.class3g.providers.personprovider/person/5");
  32. ContentValues values = new ContentValues();
  33. values.put("name", "蔣介石");
  34. contentResolver.update(updateUri, values, null, null);
  35. }
  36. public void testDelete() throws Throwable{
  37. ContentResolver contentResolver = this.getContext().getContentResolver();
  38. Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person/5");
  39. contentResolver.delete(uri, null, null);
  40. }
  41. }

4、測試(注意需要先將provider擁有者工程部署到設備上)

5、ContentProvider的監聽器

Copyright © Linux教程網 All Rights Reserved