当前位置:网站首页>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
边栏推荐
- Ctfshow - web362 (ssti)
- Creation and use of QT dynamic link library
- Halo open source project learning (VII): caching mechanism
- About the operation of unit file reading (I)
- 解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
- 玻璃体中的硫酸软骨素
- #yyds干货盘点#stringprep --- 因特网字符串预备
- How to virtualize the video frame and background is realized in a few simple steps
- MVVM模型
- QT curve / oscilloscope customplot control
猜你喜欢
[popular science] CRC verification (I) what is CRC verification?
STM32: LCD显示
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
ESP32 LVGL8. 1. Detailed migration tutorial of m5stack + lvgl + IDF (27)
Query the logistics update quantity according to the express order number
ctfshow-web361(SSTI)
STM32 learning record 0008 - GPIO things 1
ctfshow-web362(SSTI)
Tangle
WiFi ap6212 driver transplantation and debugging analysis technical notes
随机推荐
Mysqldump backup database
Daily CISSP certification common mistakes (April 11, 2022)
ESP32 LVGL8. 1 - event (event 17)
Test questions of daily safety network (February 2024)
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
Nacos cluster construction and MySQL persistence configuration
Daily network security certification test questions (April 14, 2022)
Query the logistics update quantity according to the express order number
Machine learning theory (8): model integration ensemble learning
STM32: LCD显示
STM32: LCD display
K210串口通信
Sentinel规则持久化进Nacos
Nacos作为服务配置中心实战
listener. log
【数学建模】—— 层次分析法(AHP)
Actual combat of Nacos as service configuration center
Iptables - L executes slowly
QT curve / oscilloscope customplot control
机器学习理论之(8):模型集成 Ensemble Learning