当前位置:网站首页>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
边栏推荐
- ESP32 LVGL8. 1 - msgbox message box (msgbox 28)
- Introduction to QT programming
- Chondroitin sulfate in vitreous
- ESP32 LVGL8. 1 - roller rolling (roller 24)
- Const keyword, variable and function are decorated with const
- Nacos cluster construction and MySQL persistence configuration
- 回路-通路
- Druid SQL和Security在美团点评的实践
- ESP32 LVGL8. 1 - calendar (calendar 25)
- WiFi ap6212 driver transplantation and debugging analysis technical notes
猜你喜欢

Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary

Hard core parsing promise object (do you know these seven common APIs and seven key questions?)

Setting up keil environment of GD single chip microcomputer

Practice of Druid SQL and security in meituan review

【科普】CRC校验(一)什么是CRC校验?

kettle庖丁解牛第17篇之文本文件输出

玻璃体中的硫酸软骨素

ESP32 LVGL8. 1 - input devices (input devices 18)

Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time

Creation and use of QT dynamic link library
随机推荐
ESP32 LVGL8. 1 - img picture (IMG 20)
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Deeply understand what new and make in golang are and what are the differences?
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
MVVM model
Introduction to QT programming
ctfshow-web362(SSTI)
ctfshow-web362(SSTI)
Implementation of TCP UDP communication with golang language
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
Interpretation and compilation of JVM
Jeecg boot microservice architecture
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
Keil RVMDK compiled data type
22 year flying Book manpower Kit
Machine learning theory (8): model integration ensemble learning
Ionic instruction set order from creation to packaging
CANopen STM32 transplantation
QT notes on qmap container freeing memory
os_authent_prefix