|
| 1 | +package com.example.contextprovider; |
| 2 | + |
| 3 | +import android.content.ContentProvider; |
| 4 | +import android.content.ContentValues; |
| 5 | +import android.content.UriMatcher; |
| 6 | +import android.database.Cursor; |
| 7 | +import android.database.sqlite.SQLiteDatabase; |
| 8 | +import android.net.Uri; |
| 9 | + |
| 10 | +import androidx.annotation.NonNull; |
| 11 | +import androidx.annotation.Nullable; |
| 12 | + |
| 13 | +public class dbContentProvider extends ContentProvider { |
| 14 | + |
| 15 | + public static final int TABLE1_DIR = 0; //访问表1的所有数据 |
| 16 | + public static final int TABLE1_ITEM = 1; //访问表1的单条数据 |
| 17 | + public static final int TABLE2_DIR = 2; |
| 18 | + public static final int TABLE2_ITEM = 3; |
| 19 | + |
| 20 | + static { |
| 21 | + UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 22 | + uriMatcher.addURI("com.example.app.provider","table1",TABLE1_DIR); |
| 23 | + uriMatcher.addURI("com.example.app.provider","table1/#",TABLE1_ITEM); |
| 24 | + uriMatcher.addURI("com.example.app.provider","table2",TABLE2_DIR); |
| 25 | + uriMatcher.addURI("com.example.app.provider","table2/#",TABLE2_ITEM); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + private static UriMatcher uriMatcher; |
| 30 | + public boolean onCreate() { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + @Nullable |
| 35 | + @Override |
| 36 | + public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { |
| 37 | + switch(uriMatcher.match(uri)){ |
| 38 | + case TABLE1_DIR: |
| 39 | + break; |
| 40 | + case TABLE1_ITEM: |
| 41 | + break; |
| 42 | + case TABLE2_DIR: |
| 43 | + |
| 44 | + break; |
| 45 | + case TABLE2_ITEM: |
| 46 | + //查询table2表中的单条数据 |
| 47 | + break; |
| 48 | + default: |
| 49 | + break; |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + @Nullable |
| 55 | + @Override |
| 56 | + public String getType(@NonNull Uri uri) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + @Nullable |
| 61 | + @Override |
| 62 | + public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { |
| 68 | + return 0; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { |
| 73 | + return 0; |
| 74 | + } |
| 75 | +} |
0 commit comments