当前位置:网站首页>Use of content provider

Use of content provider

2022-04-23 18:41:00 tanleicsdn

There is one saying. , The four components of content providers are basically one of the most unlicensed components . After years of development, I've only heard of this thing , I've never used this before .
This component is needed in recent projects , The requirement is to store the data of the sub application into the database (SQLite) in , Then master Launcher The application needs to read, modify and delete the data in the database of the sub application , Then I used this ContentProvider.

Analyze requirements and implementation steps :
1. Create in subapplication SQLite database , Build a good table .
2. Create a good... In the sub application ContentProvider, And define the matching Uri.
3.ContentProvider Rewrite the corresponding method in , For example, Lord Launcher Just check , Delete , The content provider of the sub application only needs to rewrite query() and delete() Method .
4. Lord Luancher Call in getContentResolver() obtain ContentResolver Object calls the corresponding method Pass in the corresponding Uri That's it .

Code :
1. Create database :

public class EvaluatDbHelper extends SQLiteOpenHelper {
// Create a database statement 
    private String createTab = "create table evaluat (text text,type text,url text)";

    public EvaluatDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    // Build table 
        db.execSQL(createTab);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

2. Create a content provider :

public class EvaluatContentProvider extends ContentProvider {
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private EvaluatDbHelper dbHelper;

    @Override
    public boolean onCreate() {
    	// Add, delete and query matching rules 
        uriMatcher.addURI("com.lee.lee.EvaluatContentProvider", "delete", 0);
        uriMatcher.addURI("com.lee.lee.EvaluatContentProvider", "query", 1);
        dbHelper = new EvaluatDbHelper(getContext(), "evaluat", null, 1);
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
    	// Rewritten query method 
        if (uriMatcher.match(uri) == 1) {
            SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();
            return writableDatabase.query("evaluat", projection, selection, selectionArgs, null, null, null);
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
    // Override delete method 
        int evaluat = 0;
        if (uriMatcher.match(uri) == 0) {
            SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();
            evaluat = writableDatabase.delete("evaluat", selection, selectionArgs);
        }
        return evaluat;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}

Since it is a component, it must be registered in the manifest file , Don't forget ,

<provider
            android:authorities="com.lee.lee.EvaluatContentProvider"
            android:name=".EvaluatContentProvider"
            android:exported="true"/>

Here we need to pay attention to android:authorities Attribute com.lee.lee.EvaluatContentProvider Need and uriMatcher.addURI(“com.lee.lee.EvaluatContentProvider”, “delete”, 0); The first parameter in is consistent Otherwise, you can't accept it .

3. Method of calling query and delete in middle note program .

// This is defined in the subroutine Uri   Fixed format is content://XXXXXX/ executable 
public static final Uri  TABLE_EV_query=Uri.parse("content://com.lee.lee.EvaluatContentProvider/query");
// Query the database of subroutines 
Cursor cursor = contentResolver.query(TABLE_EV_query, null, null, null, null, null);
 This gets the cursor 
while (cursor.moveToNext()) {
                    StudyRequestBean studyRequestBean = new StudyRequestBean();
                    studyRequestBean.content = cursor.getString(0);
                    studyRequestBean.contentType = cursor.getString(1);
                    studyRequestBean.recording = cursor.getString(2);
                }
                cursor.close();
                 In this way, the data read from the database is stored in a StudyRequestBean  Class .
// Method of deleting sub application database . I delete all here , You can also delete an item .
 contentResolver.delete(TABLE_EV_delete, null, null);

This completes the requirement of content providers to read data across applications .

版权声明
本文为[tanleicsdn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210604297255.html