首页 > 代码库 > Android中的UriMatcher、ContentUrist和ContentResolver

Android中的UriMatcher、ContentUrist和ContentResolver

       因为Uri代表了要操作的数据,所以我们很经常需要解析Uri,并从Uri中获取数据。Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris 。掌握它们的使用,会便于我们的开发工作。

       UriMatcher:用于匹配Uri,它的用法如下:
       1.首先把你需要匹配Uri路径全部给注册上,如下:

Java代码:


  1. //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)。

  2. UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

  3. //如果match()方法匹配content:.sqlite.provider.contactprovider/contact路径,返回匹配码为1

  4. uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact”, 1);
  5. //添加需要匹配uri,如果匹配就会返回匹配码

  6. //如果match()方法匹配 content.sqlite.provider.contactprovider/contact/230路径,返回匹配码为2

  7. uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact/#”, 2);
  8. //#号为通配符

复制代码

 

        2.注册完需要匹配的Uri后,就可以使用uriMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用addURI()方法传入的第三个参数,假设匹配
        content:.sqlite.provider.contactprovider/contact路径,返回的匹配码为1。
        ContentUris用于获取Uri路径后面的ID部分,它有两个比较实用的方法:
        withAppendedId(uri, id)用于为路径加上ID部分parseId(uri)方法用于从路径中获取ID部分
        ContentResolver当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法,来操作数据。

 

       ContentProvider示例程序
       我们为昨天的SQLite示例程序添加一个ContentProvider,供其他应用来访问我们的数据。
       1.为SQLite示例程序添加ContentProvider类

 

Java代码:

  1. package eoe.sqlite.provider;

  2. import com.changcheng.sqlite.MyOpenHelper;
  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 ContactContentProvider extends ContentProvider {
  11. // 通过UriMatcher匹配外部请求
  12. private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  13. // 通过openHelper进行数据库读写
  14. private MyOpenHelper openHelper;
  15. // 匹配状态常量
  16. private static final int CONTACT_LIST = 1;
  17. private static final int CONTACT = 2;

  18. // 表名
  19. private static final String tableName = "contacts";
  20. // 添加Uri
  21. static {
  22. uriMatcher.addURI("com.changcheng.sqlite.provider", "contact",CONTACT_LIST);
  23. uriMatcher.addURI("com.changcheng.sqlite.provider", "contact/#",CONTACT);
  24. }


  25. @Override
  26. public int delete(Uri uri, String selection, String[] selectionArgs) {
  27. SQLiteDatabase db = this.openHelper.getWritableDatabase();
  28. int result;


  29. switch (uriMatcher.match(uri)) {
  30. case CONTACT_LIST:
  31. result = db.delete(tableName, selection, selectionArgs);
  32. break;
  33. case CONTACT:
  34. long id = ContentUris.parseId(uri);
  35. String where = "_id=" + id;

  36. if (selection != null && !"".equals(selection)) {
  37. where = where + " and " + selection;
  38. }

  39. result = db.delete(tableName, where, selectionArgs);
  40. break;
  41. default:
  42. throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
  43. }
  44. return result;
  45. }

  46. @Override
  47. public String getType(Uri uri) {
  48. switch (uriMatcher.match(uri)) {
  49. case CONTACT_LIST:// 集合类型必须在前面加上vnd.android.cursor.dir/
  50. return "vnd.android.cursor.dir/contactlist";
  51. case CONTACT:// 非集合类型必须在前面加上vnd.android.cursor.item/
  52. return "vnd.android.cursor.item/contact";
  53. default:
  54. throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
  55. }
  56. }

  57. @Override
  58. public Uri insert(Uri uri, ContentValues values) {
  59. SQLiteDatabase db = this.openHelper.getWritableDatabase();
  60. long id;
  61. switch (uriMatcher.match(uri)) {
  62. case CONTACT_LIST:
  63. // 因为后台需要生成SQL语句,当values为null时,必须提第二个参数。生成的SQL语句才不会出错!
  64. id = db.insert(tableName, "_id", values);
  65. return ContentUris.withAppendedId(uri, id);
  66. case CONTACT:
  67. id = db.insert(tableName, "_id", values);
  68. String uriPath = uri.toString();
  69. String path = uriPath.substring(0, uriPath.lastIndexOf("/")) + id;
  70. return Uri.parse(path);
  71. default:
  72. throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
  73. }
  74. }


  75. @Override
  76. public boolean onCreate() {
  77. this.openHelper = new MyOpenHelper(this.getContext());
  78. return true;
  79. }

  80. @Override
  81. public Cursor query(Uri uri, String[] projection, String selection,
  82. String[] selectionArgs, String sortOrder) {
  83. SQLiteDatabase db = this.openHelper.getWritableDatabase();
  84. switch (uriMatcher.match(uri)) {
  85. case CONTACT_LIST:
  86. return db.query(tableName, projection, selection, selectionArgs,null, null, sortOrder);

  87. case CONTACT:
  88. long id = ContentUris.parseId(uri);
  89. String where = "_id=" + id;
  90. if (selection != null && !"".equals(selection)) {
  91. where = where + " and " + selection;
  92. }

  93. return db.query(tableName, projection, where, selectionArgs, null,null, sortOrder);
  94. default:
  95. throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
  96. }
  97. }

  98. @Override
  99. public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {
  100. SQLiteDatabase db = this.openHelper.getWritableDatabase();
  101. int result;
  102. switch (uriMatcher.match(uri)) {
  103. case CONTACT_LIST:
  104. result = db.update(selection, values, selection, selectionArgs);
  105. break;
  106. case CONTACT:
  107. long id = ContentUris.parseId(uri);
  108. String where = "_id=" + id;
  109. if (selection != null && !"".equals(selection)) {
  110. where = where + " and " + selection;
  111. }

  112. result = db.update(tableName, values, where, selectionArgs);
  113. break;
  114. default:
  115. throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
  116. }
  117. return result;
  118. }

  119. }

复制代码