当前位置:   article > 正文

如何创建Content Provider_content provider是灰色的,无法创建

content provider是灰色的,无法创建

Content Provider用来管理数据的集中存储库,Content Provider主要功能是使应用的数据可以被其他应用访问。


一、在实现Content Provider时,先考虑一下是否有必要使用Content Provider来管理数据。一般在满足一下几种情况下:

1.    需要为其他应用程序提供复杂的数据或者文件
2.    允许其他应用程序拷贝自己的应用程序复杂数据
3.    需要提供在搜索框架下提供自己的搜索建议
另外,没有必要为自己应用实现一个Content Provider使用SQLite DataBase来管理数据。

 二、如何创建一个Provider:
1.    设计原始数据,Content Provider提供两方面的数据:
a.    文件数据:如图片、视频、音频文件等,将文件保存在应用的空间中,Content Provider需要为其提供文件句柄供其他应用访问使用。
b.    结构数据,即可以保存在数据库、数组和类似结构的数据。数据将保存在一个兼容形势的表中,行表示一个实体,列表示实体的属性等。通常的做法是将这类数据保存在一个SQLite DataBase中。
2.    定义Provider的authority 、URIS和字段,如果需要Provider处理intent,定义action、extras、和flags,以及定义需要访问应用数据的权限。可以考虑将这些常量写在一个独立的类中,即Contract class。
3.    定义一个ContentProvider的子类,并实现它的方法。这个类就是ContentProvider的子类你的应用数据和其它android系统交互的接口。

4.    添加其它项,如一些示例数据、或者实现一个可以同步Provider和运数据的AbstractThreadedSyncAdapter的子类。


三、如何实现一个ContentProvider,其中上面提到1、4步忽略

2.实现Contract class

  1. package com.example.logcontentprovider;
  2. import android.net.Uri;
  3. public class LogInfo {
  4. //db info
  5. public static final String DB_NAME = "errordb";
  6. public static final String TABLE_ANR_NAME = "anrinfo";
  7. public static final String TABLE_CRASH_NAME = "crashinfo";
  8. public static final int VERSION = 1;
  9. //column name for anr
  10. public static String ANR_ID = "anr_id";
  11. public static final String ANR_ACTIVITY = "anr_activity";
  12. public static final String ANR_CAUSE = "anr_cause";
  13. public static final String ANR_CPU = "anr_cpu";
  14. //column name for crash
  15. public static String CRASH_ID = "crash_id";
  16. public static final String EXCEPTION_CLASSNAME = "exception_classname";
  17. public static final String EXCEPTION_MESSAGE = "eccption_messge";
  18. public static final String THROW_FILENAME = "throw_filename";
  19. public static final String THROW_CLASSNAME = "throw_classname";
  20. public static final String THROW_METHODNAME = "throw_methodname";
  21. public static final String THROW_LINENUMBER = "throw_linenumber";
  22. //define authority eg:com.example.<appname>.provider
  23. public static final String AUTOHORITY = "com.example.logcontentprovider";
  24. //define URIs eg: content://<authority>/<path> or  content://<authority>/<path>/<id>
  25. public static final Uri ANR_CONTENT_URI = Uri.parse("content://" + AUTOHORITY + "/" +TABLE_ANR_NAME);
  26. public static final Uri CRASH_CONTENT_URI = Uri.parse("content://" + AUTOHORITY + "/" +TABLE_CRASH_NAME);
  27. }

3.    定义一个ContentProvider的子类

  1. package com.example.logcontentprovider;
  2. import android.content.ContentProvider;
  3. import android.content.ContentUris;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.content.UriMatcher;
  7. import android.database.Cursor;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10. import android.net.Uri;
  11. import android.text.TextUtils;
  12. import android.util.Log;
  13. public class LogContentProvider extends ContentProvider{
  14. private static final UriMatcher sUriMatcher;
  15. static{
  16. sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  17. sUriMatcher.addURI(LogInfo.AUTOHORITY, LogInfo.TABLE_CRASH_NAME, 1);
  18. sUriMatcher.addURI(LogInfo.AUTOHORITY, LogInfo.TABLE_CRASH_NAME + "/#", 2);
  19. sUriMatcher.addURI(LogInfo.AUTOHORITY, LogInfo.TABLE_ANR_NAME, 3);
  20. sUriMatcher.addURI(LogInfo.AUTOHORITY, LogInfo.TABLE_ANR_NAME + "/#", 4);
  21. }
  22. //create anr table
  23. private static final String SQL_CREATE_TABLE_ANR = "CREATE TABLE "
  24. + LogInfo.TABLE_ANR_NAME + "(" + LogInfo.ANR_ID
  25. + " integer primary key autoincrement not null,"
  26. + LogInfo.ANR_ACTIVITY + " text," + LogInfo.ANR_CAUSE
  27. + " text," + LogInfo.ANR_CPU + " text);";
  28. //create crash table
  29. private static final String SQL_CREATE_TABLE_CRASH = "CREATE TABLE "
  30. + LogInfo.TABLE_CRASH_NAME + "(" + LogInfo.CRASH_ID
  31. + " integer primary key autoincrement not null,"
  32. + LogInfo.EXCEPTION_CLASSNAME + " text,"
  33. + LogInfo.EXCEPTION_MESSAGE + " text,"
  34. + LogInfo.THROW_CLASSNAME + " text,"
  35. + LogInfo.THROW_FILENAME + " text,"
  36. + LogInfo.THROW_METHODNAME + " text,"
  37. + LogInfo.THROW_LINENUMBER + " integer);";
  38. private MainDatabaseHelper mOpenHelper;
  39. private SQLiteDatabase db;
  40. public LogContentProvider() {
  41. // TODO Auto-generated constructor stub
  42. }
  43. @Override
  44. public int delete(Uri uri, String selection, String[] selectionArgs) {
  45. db = mOpenHelper.getWritableDatabase();
  46. int count = 0;
  47. String id = "";
  48. switch (sUriMatcher.match(uri)){
  49. case 1:
  50. count = db.delete(LogInfo.TABLE_CRASH_NAME, selection,
  51. selectionArgs);
  52. break;
  53. case 2:
  54. id = uri.getPathSegments().get(1);
  55. count = db.delete(LogInfo.TABLE_CRASH_NAME, 2
  56. + " = "
  57. + id
  58. + (!TextUtils.isEmpty(LogInfo.CRASH_ID = "?") ? "AND("
  59. + selection + ')' : ""), selectionArgs);
  60. break;
  61. case 3:
  62. count = db.delete(LogInfo.TABLE_ANR_NAME, selection,
  63. selectionArgs);
  64. break;
  65. case 4:
  66. id = uri.getPathSegments().get(1);
  67. count = db.delete(LogInfo.TABLE_ANR_NAME, 2
  68. + " = "
  69. + id
  70. + (!TextUtils.isEmpty(LogInfo.ANR_ID = "?") ? "AND("
  71. + selection + ')' : ""), selectionArgs);
  72. break;
  73. default:
  74. Log.e("zy", "not usfull uri = " + uri);
  75. }
  76. getContext().getContentResolver().notifyChange(uri, null);
  77. return count;
  78. }
  79. @Override
  80. public String getType(Uri uri) {
  81. // TODO Auto-generated method stub
  82. return null;
  83. }
  84. @Override
  85. public Uri insert(Uri uri, ContentValues values) {
  86. db = mOpenHelper.getWritableDatabase();
  87. long rowId = 0 ;
  88. Uri insertUri = null;
  89. switch (sUriMatcher.match(uri)){
  90. case 1:
  91. rowId = db.insert(LogInfo.TABLE_CRASH_NAME, LogInfo.CRASH_ID, values);
  92. if(rowId > 0){
  93. insertUri = ContentUris.withAppendedId(LogInfo.ANR_CONTENT_URI, rowId);
  94. }
  95. break;
  96. case 2:
  97. Log.e("zy", "this uri is not usfull for insert, uri = " + uri );
  98. break;
  99. case 3:
  100. rowId = db.insert(LogInfo.TABLE_ANR_NAME, LogInfo.ANR_ID, values);
  101. if(rowId > 0){
  102. insertUri = ContentUris.withAppendedId(LogInfo.ANR_CONTENT_URI, rowId);
  103. }
  104. break;
  105. case 4:
  106. Log.e("zy", "this uri is not usfull for insert, uri = " + uri );
  107. break;
  108. default:
  109. Log.e("zy", "not usfull uri = " + uri);
  110. }
  111. if(rowId > 0){
  112. getContext().getContentResolver().notifyChange(insertUri, null);
  113. }
  114. return insertUri;
  115. }
  116. @Override
  117. public boolean onCreate() {
  118. mOpenHelper = new MainDatabaseHelper(this.getContext());
  119. return false;
  120. }
  121. @Override
  122. public Cursor query(Uri uri, String[] projection, String selection,
  123. String[] selectionArgs, String sortOrder) {
  124. db = mOpenHelper.getWritableDatabase();
  125. Cursor c = null;
  126. String id = "";
  127. switch (sUriMatcher.match(uri)) {
  128. case 1:
  129. c = db.query(LogInfo.TABLE_CRASH_NAME, projection, selection,
  130. selectionArgs, null, null, sortOrder);
  131. break;
  132. case 2:
  133. id = uri.getPathSegments().get(1);
  134. c = db.query(LogInfo.TABLE_CRASH_NAME, projection, LogInfo.ANR_ID
  135. + "="
  136. + id
  137. + (!TextUtils.isEmpty(selection) ? "AND(" + selection + ')'
  138. : ""), selectionArgs, null, null, sortOrder);
  139. break;
  140. case 3:
  141. c = db.query(LogInfo.TABLE_ANR_NAME, projection, selection,
  142. selectionArgs, null, null, sortOrder);
  143. break;
  144. case 4:
  145. c = db.query(LogInfo.TABLE_ANR_NAME, projection, LogInfo.ANR_ID
  146. + "="
  147. + id
  148. + (!TextUtils.isEmpty(selection) ? "AND(" + selection + ')'
  149. : ""), selectionArgs, null, null, sortOrder);
  150. break;
  151. default:
  152. Log.e("zy", "not usfull uri = " + uri);
  153. }
  154. return c;
  155. }
  156. @Override
  157. public int update(Uri uri, ContentValues values, String selection,
  158. String[] selectionArgs) {
  159. db = mOpenHelper.getWritableDatabase();
  160. int count = 0;
  161. String id = "";
  162. switch (sUriMatcher.match(uri)) {
  163. case 1:
  164. count = db.update(LogInfo.TABLE_CRASH_NAME, values, selection,
  165. selectionArgs);
  166. break;
  167. case 2:
  168. id = uri.getPathSegments().get(1);
  169. count = db.update(
  170. LogInfo.TABLE_CRASH_NAME,
  171. values,
  172. LogInfo.CRASH_ID
  173. + "="
  174. + id
  175. + (!TextUtils.isEmpty(selection) ? "AND("
  176. + selection + ")" : ""), selectionArgs);
  177. break;
  178. case 3:
  179. count = db.update(LogInfo.TABLE_ANR_NAME, values, selection,
  180. selectionArgs);
  181. break;
  182. case 4:
  183. id = uri.getPathSegments().get(1);
  184. count = db.update(
  185. LogInfo.TABLE_ANR_NAME,
  186. values,
  187. LogInfo.ANR_ID
  188. + "="
  189. + id
  190. + (!TextUtils.isEmpty(selection) ? "AND("
  191. + selection + ")" : ""), selectionArgs);
  192. break;
  193. default:
  194. Log.e("zy", "not usfull uri = " + uri);
  195. }
  196. return count;
  197. }
  198. protected static final class MainDatabaseHelper extends SQLiteOpenHelper {
  199. public MainDatabaseHelper(Context context) {
  200. super(context, LogInfo.DB_NAME, null, LogInfo.VERSION);
  201. }
  202. @Override
  203. public void onCreate(SQLiteDatabase db) {
  204. db.execSQL(SQL_CREATE_TABLE_CRASH);
  205. db.execSQL(SQL_CREATE_TABLE_ANR);
  206. }
  207. @Override
  208. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  209. // TODO Auto-generated method stub
  210. }
  211. }
  212. }

另外:别忘了在AndroidManifest.xml文件中注册:

  1. <provider android:name="com.example.logcontentprovider.LogContentProvider"
  2. android:authorities="com.example.logcontentprovider"
  3. android:permission="true">
  4. </provider>

四、使用ContentResolver来访问ContentProvider中的数据库:

  1.  private Uri saveErrorInfo(){      
  2.       ContentResolver resolver = getContentResolver();
  3.       values.put(LogInfo.EXCEPTION_CLASSNAME, “a1”);
  4.       values.put(LogInfo.EXCEPTION_MESSAGE,“a2”);
  5.       values.put(LogInfo.THROW_CLASSNAME, “a3”);
  6.       values.put(LogInfo.THROW_FILENAME, “a4”);
  7.       values.put(LogInfo.THROW_METHODNAME, “a5”);
  8.       values.put(LogInfo.THROW_LINENUMBER, “a6”);
  9.       Uri uri = Uri.parse("content://com.example.logcontentprovider/crashinfo");
  10.       return resolver.insert(uri, values); //返回insert成功后对应行的Uri eg:content://<authority>/<path>/<id>
  11. }

成功后可以导出db文件查看到插入到数据库中的数据.


以上,即完成了一个ContentProvider的创建和使用过程。





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/941054
推荐阅读
相关标签
  

闽ICP备14008679号