实例2

ContentProviderDemo.zip
image.png
image.png
image.png
image.png

在自己的用户目录里边创建的数据库,自己提供provider,自己访问自己的数据库

效果

image.png

AndroidManifest.xml

image.png

MyContentProvider.java

  1. package com.example.contentproviderdemo;
  2. import android.content.ContentProvider;
  3. import android.content.ContentUris;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.content.IntentFilter;
  7. import android.content.UriMatcher;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.database.sqlite.SQLiteOpenHelper;
  11. import android.net.Uri;
  12. import android.util.Log;
  13. public class MyContentProvider extends ContentProvider {
  14. private DBHelper mDBHelper;
  15. private SQLiteDatabase mDB;
  16. public MyContentProvider() {
  17. }
  18. //定义为静态的,可以在静态域里边初始化
  19. public static UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  20. private static final String authority = "MyContentProvider";
  21. private static final int PERSONS = 0x111;
  22. private static final int PERSONS_ID = 0x222;
  23. //静态域初始化
  24. static {
  25. //content://MyContentProvider/person
  26. sUriMatcher.addURI(authority,"person",PERSONS);
  27. //content://MyContentProvider/person/id
  28. sUriMatcher.addURI(authority,"person/#",PERSONS_ID);
  29. }
  30. @Override
  31. public int delete(Uri uri, String selection, String[] selectionArgs) {
  32. // Implement this to handle requests to delete one or more rows.
  33. throw new UnsupportedOperationException("Not yet implemented");
  34. }
  35. @Override
  36. public String getType(Uri uri) {
  37. // TODO: Implement this to handle requests for the MIME type of the data
  38. // at the given URI.
  39. //vnd.android.cursor.item/ 单记录
  40. //vnd.android.cursor.dir/ 多记录
  41. String type = null;
  42. int code = sUriMatcher.match(uri);
  43. switch (code) {
  44. case PERSONS_ID:
  45. type = "vnd.android.cursor.item/person";
  46. break;
  47. case PERSONS:
  48. type = "vnd.android.cursor.dir/person";
  49. break;
  50. default:
  51. break;
  52. }
  53. return type;
  54. }
  55. @Override
  56. public Uri insert(Uri uri, ContentValues values) {
  57. // TODO: Implement this to handle requests to insert a new row.
  58. mDB = mDBHelper.getReadableDatabase();
  59. long id = mDB.insert("person",null,values);
  60. //返回组合好的uri 类似://content://MyContentProvider/person/id
  61. return ContentUris.withAppendedId(uri,id);
  62. }
  63. @Override
  64. public boolean onCreate() {
  65. // TODO: Implement this to initialize your content provider on startup.
  66. mDBHelper = new DBHelper(getContext());
  67. return true ;
  68. }
  69. @Override
  70. public Cursor query(Uri uri, String[] projection, String selection,
  71. String[] selectionArgs, String sortOrder) {
  72. // TODO: Implement this to handle query requests from clients.
  73. Log.d("15pb","MyContentProvider::query");
  74. mDB = mDBHelper.getReadableDatabase();
  75. int code = sUriMatcher.match(uri);
  76. if (code==PERSONS){
  77. return mDB.query("person",new String[]{"_id","name"},null,null,null,null,null);
  78. }else if(code==PERSONS_ID){
  79. long id = ContentUris.parseId(uri);
  80. return mDB.query("person",new String[]{"_id","name"},"_id=?",new String[]{""+id},null,null,null);
  81. }
  82. return null;
  83. }
  84. @Override
  85. public int update(Uri uri, ContentValues values, String selection,
  86. String[] selectionArgs) {
  87. // TODO: Implement this to handle requests to update one or more rows.
  88. throw new UnsupportedOperationException("Not yet implemented");
  89. }
  90. }

DBHelper.java

  1. package com.example.contentproviderdemo;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import androidx.annotation.Nullable;
  6. public class DBHelper extends SQLiteOpenHelper {
  7. public DBHelper(@Nullable Context context) {
  8. super(context, "mydata.db", null, 1);
  9. }
  10. public DBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
  11. super(context, name, factory, version);
  12. }
  13. @Override
  14. public void onCreate(SQLiteDatabase db) {
  15. //创建表
  16. db.execSQL("create table person(_id integer primary key autoincrement,name varchar(20))");
  17. }
  18. @Override
  19. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  20. }
  21. }

MainActivity.java

  1. package com.example.contentproviderdemo;
  2. import android.content.ContentResolver;
  3. import android.content.ContentValues;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. public class MainActivity extends AppCompatActivity {
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. }
  16. public void btnClick(View view) {
  17. // 1. 获取内容解析者
  18. ContentResolver resolver = getContentResolver();
  19. // 2. 创建URI
  20. Uri uri = Uri.parse("content://"+"MyContentProvider");
  21. // 3. 查询
  22. Cursor cursor = resolver.query(uri,null,null,null,null,null);
  23. Log.d("15pb","cursor:"+cursor);
  24. }
  25. public void btnClick1(View view) {
  26. // 1. 获取内容解析者
  27. ContentResolver resolver = getContentResolver();
  28. // 2. 创建URI
  29. Uri uri = Uri.parse("content://"+"MyContentProvider");
  30. // 3. 创建数据
  31. for (int i = 0; i < 100; i++) {
  32. ContentValues values = new ContentValues();
  33. values.put("_id",""+i);
  34. values.put("name","王大锤"+i);
  35. resolver.insert(uri,values);
  36. }
  37. //4.查询数据
  38. Uri uri1 = Uri.parse("content://"+"MyContentProvider"+"/person");
  39. Cursor cursor = resolver.query(uri1,null,null,null,null,null);
  40. Log.d("15pb","cursor:"+cursor);
  41. while (cursor.moveToNext()){
  42. int id = cursor.getInt(0);
  43. String name = cursor.getString(1);
  44. Log.d("15pb","id:"+id+"name="+name);
  45. }
  46. }
  47. public void btnClick2(View view) {
  48. // 1. 获取内容解析者
  49. ContentResolver resolver = getContentResolver();
  50. // 2. 创建URI
  51. Uri uri = Uri.parse("content://"+"MyContentProvider");
  52. // 3. 创建数据
  53. for (int i = 0; i < 100; i++) {
  54. ContentValues values = new ContentValues();
  55. values.put("_id",""+i);
  56. values.put("name","王大锤"+i);
  57. resolver.insert(uri,values);
  58. }
  59. //4.查询数据
  60. Uri uri1 = Uri.parse("content://"+"MyContentProvider"+"/person/10");
  61. Cursor cursor = resolver.query(uri1,null,null,null,null,null);
  62. Log.d("15pb","cursor:"+cursor);
  63. while (cursor.moveToNext()){
  64. int id = cursor.getInt(0);
  65. String name = cursor.getString(1);
  66. Log.d("15pb","id:"+id+"name="+name);
  67. }
  68. }
  69. }

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/btn1"
  11. android:onClick="btnClick"
  12. android:text="访问内容提供者"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"></Button>
  15. <Button
  16. android:id="@+id/btn2"
  17. android:onClick="btnClick1"
  18. android:text="访问内容提供者-插入数据"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"></Button>
  21. <Button
  22. android:id="@+id/btn3"
  23. android:onClick="btnClick2"
  24. android:text="访问内容提供者-查询指定数据"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"></Button>
  27. </LinearLayout>

ContentProvider案例分析

ContentProvider案例.pdf