当前位置:网站首页>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
边栏推荐
- Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
- Actual combat of Nacos as service configuration center
- Excel intercept text
- Configure iptables
- Mysqldump backup database
- QT excel operation summary
- ESP32 LVGL8. 1 - img picture (IMG 20)
- With the use of qchart, the final UI interface can be realized. The control of qweight can be added and promoted to a user-defined class. Only the class needs to be promoted to realize the coordinate
- Can filter
- ESP32 LVGL8. 1 - checkbox (checkbox 23)
猜你喜欢
ctfshow-web361(SSTI)
STM32: LCD显示
ESP32 LVGL8. 1 - textarea text area (textarea 26)
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
ESP32 LVGL8. 1 - arc (arc 19)
Halo open source project learning (VII): caching mechanism
Query the logistics update quantity according to the express order number
MVVM模型
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
ESP32 LVGL8. 1 - input devices (input devices 18)
随机推荐
Teach you to quickly rename folder names in a few simple steps
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Ctfshow - web362 (ssti)
Interpretation and compilation of JVM
Can filter
Imx6 debugging LVDS screen technical notes
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Actual combat of Nacos as service configuration center
ctfshow-web362(SSTI)
Daily CISSP certification common mistakes (April 19, 2022)
STM32: LCD显示
os_ authent_ Prefix
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
Loop path
Daily CISSP certification common mistakes (April 14, 2022)
Ionic 从创建到打包指令集顺序
CANopen STM32 transplantation
RPM包管理
The connection of imx6 network port is unstable after power on
Daily network security certification test questions (April 15, 2022)