备注:
ContentProvIDer1进程一代码:https://github.com/zengyuan/ContentProvider1Mode
ContentProvIDer1进程二代码:https://github.com/zengyuan/ContentProvider2Mode
AndroID Service 启动方式,生命周期和应用场景详解: https://www.cnblogs.com/finn21/p/11652482.html
iOS APP打包上传审核和 客服咨询流程(审核被拒,账号问题等):https://www.cnblogs.com/finn21/p/11084272.html
注意,最主要是provIDer的配置,以及URl的provIDer地址要相同;
一,ContentProvIDer 主要用于不同的应用程序之间实现数据共享功能!
主要分为6个:
1.使用sqlite技术,创建好数据库和数据表;
2.新建类继承ContentProvIDer,重写6个抽象方法(通过这六个方法对数据库进行 *** 作);
3.在Manifest中注册provIDer;
4.创建UriMatcher,定义Uri规则,添加注册数据库链表的名称;
5.ContentResolver对ContentProvIDer中共享的数据进行增删改查 *** 作;
6.获取数据并修改等(进程一进程二增删改查方式一样);
二,进程一代码伺候:
第一步创建数据库(SQL语句直接执行的):
package com.saiyi.contentprovIDerdemo1;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;/** * <pre> * author : Finn * e-mail : 892603597@qq.com * time : 2019/10/14 * desc : https://www.cnblogs.com/finn21/ * </pre> */public class DBHelper extends sqliteOpenHelper { // 数据库名 private static final String DATABASE_name = "Finn.db"; // 链表名称 public static final String USER_table_name = "user"; public static final String JOB_table_name = "job"; private static final int DATABASE_VERSION = 1; //数据库版本号 public DBHelper(Context context) { super(context,DATABASE_name, null, DATABASE_VERSION); } @OverrIDe public voID onCreate(sqliteDatabase db) { // 创建两个表格:用户表 和职业表 db.execsql("CREATE table IF NOT EXISTS " + USER_table_name + "(_ID INTEGER PRIMARY KEY autoINCREMENT," + " name TEXT)"); db.execsql("CREATE table IF NOT EXISTS " + JOB_table_name + "(_ID INTEGER PRIMARY KEY autoINCREMENT," + " job TEXT)"); } @OverrIDe public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { } @OverrIDe public voID onopen(sqliteDatabase db) { super.onopen(db); } @OverrIDe public synchronized voID close() { super.close(); }}
第一步创建数据库(通过自行封装之后的数据库):
package com.saiyi.contentprovIDerdemo1;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;/** * <pre> * author : Finn * e-mail : 892603597@qq.com * time : 2019/10/14 * desc : https://www.cnblogs.com/finn21/ * </pre> */public class MyDataBaseHelper extends sqliteOpenHelper { private static final String DATABASE_name = "KfbFinnSS.db"; private static final int DATABASE_VERSION = 1; public static final String USER_ID = "userID_ID";// 自动增长表ID /***.用户名(存储车型af)**/ public static final String table_USER_AF = "table_user_af"; public static final String USER_USER_AF = "car_user_name_af";//用户名 public static final String USER_CAR_AZ_AF = "car_user_az_af";//字符分段 public static final String USER_CAR_name_AF = "car_kfb_name_af";// 车名称 public static final String USER_CAR_TYPE_AF = "car_user_cartype_af";// 车型号 public static final String USER_CAR_DATE_AF = "car_kfb_date_af";// 年份 public static final String USER_CAR_LENGM_AF = "car_kfb_lengmei_af";// 冷媒量 public MyDataBaseHelper(Context context) { super(context,DATABASE_name, null, DATABASE_VERSION); } @OverrIDe public voID onCreate(sqliteDatabase db) {/** AF * */ db.execsql("CREATE table " + table_USER_AF + " (" + USER_ID + " INTEGER PRIMARY KEY," + USER_USER_AF + " INTEGER NOT Null," + USER_CAR_AZ_AF + " TEXT," + USER_CAR_name_AF + " TEXT," + USER_CAR_TYPE_AF + " TEXT," + USER_CAR_DATE_AF + " TEXT NOT Null," + USER_CAR_LENGM_AF + " INTEGER default 0);"); } @OverrIDe public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { //数据库升级启动该方法 } @OverrIDe public synchronized voID close() { super.close(); } @OverrIDe public voID onopen(sqliteDatabase db) { super.onopen(db); }}
package com.saiyi.contentprovIDerdemo1;import androID.content.ContentValues;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import java.util.ArrayList;import java.util.List;/** * <pre> * author : Finn * e-mail : 892603597@qq.com * time : 2019/10/14 * desc : https://www.cnblogs.com/finn21/ * </pre> */public class MyDataBase { private static final String TAG = MyDataBase.class.getSimplename(); private static MyDataBaseHelper databaseHelper; private static MyDataBase mydisplacementBase; private static String USER_name = "13006602877"; private static String table_USER_name = "KFB"; private MyDataBase(Context context) { databaseHelper = new MyDataBaseHelper(context); } public static MyDataBase getInstance(Context context) { if (mydisplacementBase == null) { mydisplacementBase = new MyDataBase(context.getApplicationContext()); } return mydisplacementBase; } /** * 关闭当前的db * * @param db */ synchronized voID closesqlDB(sqliteDatabase db) { sqliteDatabase.releaseMemory(); if (db != null && db.isopen()) { db.close(); db = null; } } /** * 通知本地数据库有变化 */ public interface InotifyDBhasChange { int CODE_ERR_REPET = 0; voID onChange(Object... obj); voID err(int... code); } /** * --------------------------------------添加车名AF--------------------------------------- */ public synchronized voID InsertCaraF(CarInfo info) { sqliteDatabase writeDB = databaseHelper.getWritableDatabase(); Cursor cursor = writeDB.query(MyDataBaseHelper.table_USER_AF, new String[]{MyDataBaseHelper.USER_ID}, MyDataBaseHelper.USER_USER_AF + " = ? ", new String[]{table_USER_name}, null, null, null); cursor.close(); ContentValues cv = new ContentValues(); cv.put(MyDataBaseHelper.USER_USER_AF, table_USER_name); cv.put(MyDataBaseHelper.USER_CAR_AZ_AF, info.getAz()); cv.put(MyDataBaseHelper.USER_CAR_name_AF, info.getCarname()); cv.put(MyDataBaseHelper.USER_CAR_TYPE_AF, info.getCarType()); cv.put(MyDataBaseHelper.USER_CAR_DATE_AF, info.getCarDate()); cv.put(MyDataBaseHelper.USER_CAR_LENGM_AF, info.getCarLengM()); try { writeDB.insert(MyDataBaseHelper.table_USER_AF, null, cv);//插入数据 } catch (Exception e) { return; } finally { closesqlDB(writeDB); } } /** * 查询所有用户 */ public synchronized List<CarInfo> queryListAF() { List<CarInfo> List = new ArrayList<>(); sqliteDatabase readDB = databaseHelper.getReadableDatabase(); Cursor cursor = readDB.query(MyDataBaseHelper.table_USER_AF, null, MyDataBaseHelper.USER_USER_AF + " = ? ", new String[]{table_USER_name}, null, null, MyDataBaseHelper.USER_ID, null); try {// int user = cursor.getColumnIndex(MyDataBaseHelper.USER_USER_AF); int pswd = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_AZ_AF); int icon = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_name_AF); int nickname = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_TYPE_AF); int vertion = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_DATE_AF); int veron = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_LENGM_AF); while (cursor.movetoNext()) { CarInfo info = new CarInfo( cursor.getString(pswd), cursor.getString(icon), cursor.getString(nickname), cursor.getString(vertion), cursor.getString(veron) ); List.add(info); } } catch (Exception e) { } finally { if (cursor != null) { cursor.close(); } closesqlDB(readDB); } return List; } public Cursor cursor; public sqliteDatabase readDB; public synchronized Cursor queryCursor() { readDB = databaseHelper.getReadableDatabase(); cursor = readDB.query(MyDataBaseHelper.table_USER_AF, null, MyDataBaseHelper.USER_USER_AF + " = ? ", new String[]{table_USER_name}, null, null, MyDataBaseHelper.USER_ID, null); return cursor; } public synchronized voID CloseCursor() { if (cursor != null) { cursor.close(); } closesqlDB(readDB); } /** * 查询所有用户 */ public synchronized Cursor queryListAFA() { sqliteDatabase readDB = databaseHelper.getReadableDatabase(); Cursor cursor = readDB.query(MyDataBaseHelper.table_USER_AF, null, MyDataBaseHelper.USER_USER_AF + " = ? ", new String[]{table_USER_name}, null, null, MyDataBaseHelper.USER_ID, null); try { } catch (Exception e) { } finally { if (cursor != null) { cursor.close(); } closesqlDB(readDB); } return cursor; } /** * 查询AF特定名称的车类型 */ public synchronized List<CarInfo> queryListAFCarname(String Carname) { List<CarInfo> List = new ArrayList<>(); sqliteDatabase readDB = databaseHelper.getReadableDatabase(); Cursor cursor = readDB.query(MyDataBaseHelper.table_USER_AF, null, MyDataBaseHelper.USER_USER_AF + " = ? and " + MyDataBaseHelper.USER_CAR_name_AF + " = ? ", new String[]{table_USER_name, Carname}, null, null, MyDataBaseHelper.USER_ID, null); try {// int user = cursor.getColumnIndex(MyDataBaseHelper.USER_USER_AF); int pswd = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_AZ_AF); int icon = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_name_AF); int nickname = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_TYPE_AF); int vertion = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_DATE_AF); int veron = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_LENGM_AF); while (cursor.movetoNext()) { CarInfo info = new CarInfo( cursor.getString(pswd), cursor.getString(icon), cursor.getString(nickname), cursor.getString(vertion), cursor.getString(veron) ); List.add(info); } } catch (Exception e) { } finally { if (cursor != null) { cursor.close(); } closesqlDB(readDB); } return List; } /** * 查询AF特定名称和车类型 的生产年份 */ public synchronized List<CarInfo> queryListAFCarnameYeas(String Carname,String CarType) { List<CarInfo> List = new ArrayList<>(); sqliteDatabase readDB = databaseHelper.getReadableDatabase(); Cursor cursor = readDB.query(MyDataBaseHelper.table_USER_AF, null, MyDataBaseHelper.USER_USER_AF + " = ? and " + MyDataBaseHelper.USER_CAR_name_AF + " = ? and " + MyDataBaseHelper.USER_CAR_TYPE_AF + " = ? ", new String[]{table_USER_name, Carname,CarType}, null, null, MyDataBaseHelper.USER_ID, null); try {// int user = cursor.getColumnIndex(MyDataBaseHelper.USER_USER_AF); int pswd = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_AZ_AF); int icon = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_name_AF); int nickname = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_TYPE_AF); int vertion = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_DATE_AF); int veron = cursor.getColumnIndex(MyDataBaseHelper.USER_CAR_LENGM_AF); while (cursor.movetoNext()) { CarInfo info = new CarInfo( cursor.getString(pswd), cursor.getString(icon), cursor.getString(nickname), cursor.getString(vertion), cursor.getString(veron) ); List.add(info); } } catch (Exception e) { } finally { if (cursor != null) { cursor.close(); } closesqlDB(readDB); } return List; } /** * 删除列表数据 */ public synchronized voID DeletInfoAF(InotifyDBhasChange dbChange) { sqliteDatabase writeDB = databaseHelper.getWritableDatabase(); try { writeDB.delete( MyDataBaseHelper.table_USER_AF, MyDataBaseHelper.USER_USER_AF + " = ? " , new String[]{table_USER_name}); } catch (Exception e) { dbChange.err(); return; } finally { closesqlDB(writeDB); } dbChange.onChange(); }}
第二步:2.新建类继承ContentProvIDer,重写6个抽象方法
package com.saiyi.contentprovIDerdemo1;import androID.content.ContentProvIDer;import androID.content.ContentValues;import androID.content.Context;import androID.content.UriMatcher;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.net.Uri;import androID.util.Log;/** * <pre> * author : Finn * e-mail : 892603597@qq.com * time : 2019/10/14 * desc : https://www.cnblogs.com/finn21/ * </pre> */public class BaseContentProvIDer extends ContentProvIDer{ private Context mContext; DBHelper mDbHelper = null; sqliteDatabase db = null; public static final String autoHORITY = "Finn.zy.myprovIDer"; // 设置ContentProvIDer的唯一标识 public static final int User_Code = 1; public static final int Job_Code = 2; public static final int AF_Code = 32; // UriMatcher类使用:在ContentProvIDer 中注册URI private static final UriMatcher mMatcher; public static DataBaseMonitor mListener = null; public static voID setDataBaseMonitorListener(DataBaseMonitor Listener) { mListener = Listener; } static{ mMatcher = new UriMatcher(UriMatcher.NO_MATCH); // 初始化 mMatcher.addURI(autoHORITY,DBHelper.USER_table_name, User_Code); // 若URI资源路径 = "content://" + autoHORITY +"/user" mMatcher.addURI(autoHORITY, DBHelper.JOB_table_name, Job_Code);// 若URI资源路径 = "content://" + autoHORITY +"/job" mMatcher.addURI(autoHORITY, MyDataBaseHelper.table_USER_AF, AF_Code);// 若URI资源路径 = "content://" + autoHORITY +"/job" // 固定前缀 "content://" + autoHORITY,数据库链表的名称,则返回注册码User_Code //固定前缀 "content://" + autoHORITY ,数据库链表的名称,则返回注册码Job_Code } // 以下是ContentProvIDer的6个方法 @OverrIDe public boolean onCreate() { //该方法在ContentProvIDer创建后就会被调用,AndroID开机后, // ContentProvIDer在其它应用第一次访问它时才会被创建,同时,要讲返回值修改成true。 mContext = getContext(); // 在ContentProvIDer创建时对数据库进行初始化 // 运行在主线程,故不能做耗时 *** 作,此处仅作展示 mDbHelper = new DBHelper(getContext()); db = mDbHelper.getWritableDatabase(); // 初始化两个表的数据(先清空两个表,再各加入一个记录) db.execsql("delete from user"); db.execsql("insert into user values(1,'Carson');"); db.execsql("insert into user values(2,'Kobe');"); MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo("宝马","奔驰","路虎","宾利","保时捷")); MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo("宝马1","奔驰2","路虎3","宾利4","保时捷5")); return true; } public static final int ITEMS = 1; public static final int ITEMS_ID = 2;// 如果 *** 作的数据属于集合类型,那么MIME类型字符串应该以vnd.androID.cursor.dir/开头。// 例如:要得到所有person记录的Uri为content://com.cfox.contentprovID.PersonProvIDer/person// 那么返回的MIME类型字符串应该为:"vnd.androID.cursor.dir/person" // 如果要 *** 作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.androID.cursor.item/开头// 例如:得到ID为10的person记录,Uri为content://com.cfox.contentprovID.PersonProvIDer/person/10// 那么返回的MIME类型字符串为:"vnd.androID.cursor.item/person" @OverrIDe public String getType(Uri uri) { return null; } @OverrIDe public Uri insert(Uri uri, ContentValues values) { //该方法用于供外部应用往ContentProvIDer添加数据。 // 根据URI匹配 URI_CODE,从而匹配ContentProvIDer中相应的表名 // 该方法在最下面 String table = gettablename(uri); Log.e("sdfsdf",table+"====="+MyDataBaseHelper.table_USER_AF); if(table != null && !MyDataBaseHelper.table_USER_AF.equals(table)){ // 向该表添加数据 db.insert(table, null, values); // 当该URI的ContentProvIDer数据发生变化时,通知外界(即访问该ContentProvIDer数据的访问者) mContext.getContentResolver().notifyChange(uri, null); return uri; }else{ FinnInsert(uri,values); return uri; }// // 通过ContentUris类从URL中获取ID// long personID = ContentUris.parseID(uri);// System.out.println(personID); } @OverrIDe public int delete(Uri uri, String selection, String[] selectionArgs) { //该方法用于供外部应用从ContentProvIDer删除数据。 String tablename = gettablename(uri); if(!MyDataBaseHelper.table_USER_AF.equals(tablename)){ //使用原生函数的数据库 return 0; }else{ //使用封装之后的数据库 return 0; } } @OverrIDe public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } /** * 查询数据 */ @OverrIDe public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { //该方法用于供外部应用从ContentProvIDer中获取数据。 // 根据URI匹配 URI_CODE,从而匹配ContentProvIDer中相应的表名 // 该方法在最下面 String tablename = gettablename(uri); Finnquery(tablename);// // 通过ContentUris类从URL中获取ID// long personID = ContentUris.parseID(uri);// System.out.println(personID); // 查询数据+ if(!MyDataBaseHelper.table_USER_AF.equals(tablename)){ //使用原生函数的数据库 return db.query(tablename,projection,selection,selectionArgs,null,null,sortOrder,null); }else{ //使用封装之后的数据库 return MyDataBase.getInstance(mContext).queryCursor(); } } /** * 根据URI匹配 URI_CODE,从而匹配ContentProvIDer中相应的表名 */ public static final String gettablename(Uri uri){ String tablename = null; switch (mMatcher.match(uri)) { case User_Code: tablename = DBHelper.USER_table_name; break; case Job_Code: tablename = DBHelper.JOB_table_name; break; case AF_Code: tablename = MyDataBaseHelper.table_USER_AF; break; } return tablename; } public voID FinnInsert(Uri uri, ContentValues values) { //自行封装方法添加数据库 String table = gettablename(uri); switch (table){//根据表名 插入数据 case MyDataBaseHelper.table_USER_AF://通过KEY获取value MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo( values.get("car_user_az_af").toString(), values.get("car_kfb_name_af").toString(), values.get("car_user_cartype_af").toString(), values.get("car_kfb_date_af").toString(), values.get("car_kfb_lengmei_af").toString() ));// MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo(// values.get(MyDataBaseHelper.USER_CAR_AZ_AF).toString(), values.get(MyDataBaseHelper.USER_CAR_name_AF).toString(),// values.get(MyDataBaseHelper.USER_CAR_TYPE_AF).toString(), values.get(MyDataBaseHelper.USER_CAR_DATE_AF).toString(),// values.get(MyDataBaseHelper.USER_CAR_LENGM_AF).toString()// )); break; default: break; } } public voID Finnquery(String tablename) { //自行封装方法添加数据库 switch (tablename){//根据表名 插入数据 case MyDataBaseHelper.table_USER_AF: mListener.OnReList(MyDataBase.getInstance(mContext).queryListAF()); break; default: break; } }}
第三步:在Manifest中注册provIDer;
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" package="com.saiyi.contentprovIDerdemo1"> <application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:roundIcon="@mipmap/ic_launcher_round" androID:supportsRtl="true" androID:theme="@style/Apptheme" tools:ignore="GoogleAppIndexingWarning"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--authoritIEs地址和 BaseContentProvIDer中的autoHORITY要保持一致--> <provIDer androID:name=".BaseContentProvIDer" androID:authoritIEs="Finn.zy.myprovIDer" androID:enabled="true" androID:exported="true" /> </application></manifest>
package com.saiyi.contentprovIDerdemo1;总结
import androID.content.ContentProvIDer;
import androID.content.ContentValues;
import androID.content.Context;
import androID.content.UriMatcher;
import androID.database.Cursor;
import androID.database.sqlite.sqliteDatabase;
import androID.net.Uri;
import androID.util.Log;
/**
* <pre>
* author : Finn
* e-mail : 892603597@qq.com
* time : 2019/10/14
* desc : https://www.cnblogs.com/finn21/
* </pre>
*/
public class BaseContentProvIDer extends ContentProvIDer{
private Context mContext;
DBHelper mDbHelper = null;
sqliteDatabase db = null;
public static final String autoHORITY = "Finn.zy.myprovIDer";
// 设置ContentProvIDer的唯一标识
public static final int User_Code = 1;
public static final int Job_Code = 2;
public static final int AF_Code = 32;
// UriMatcher类使用:在ContentProvIDer 中注册URI
private static final UriMatcher mMatcher;
public static DataBaseMonitor mListener = null;
public static voID setDataBaseMonitorListener(DataBaseMonitor Listener) {
mListener = Listener;
}
static{
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// 初始化
mMatcher.addURI(autoHORITY,DBHelper.USER_table_name, User_Code); // 若URI资源路径 = "content://" + autoHORITY +"/user"
mMatcher.addURI(autoHORITY, DBHelper.JOB_table_name, Job_Code);// 若URI资源路径 = "content://" + autoHORITY +"/job"
mMatcher.addURI(autoHORITY, MyDataBaseHelper.table_USER_AF, AF_Code);// 若URI资源路径 = "content://" + autoHORITY +"/job"
// 固定前缀 "content://" + autoHORITY,数据库链表的名称,则返回注册码User_Code
//固定前缀 "content://" + autoHORITY ,数据库链表的名称,则返回注册码Job_Code
}
// 以下是ContentProvIDer的6个方法
@OverrIDe
public boolean onCreate() {
//该方法在ContentProvIDer创建后就会被调用,AndroID开机后,
// ContentProvIDer在其它应用第一次访问它时才会被创建,同时,要讲返回值修改成true。
mContext = getContext();
// 在ContentProvIDer创建时对数据库进行初始化
// 运行在主线程,故不能做耗时 *** 作,此处仅作展示
mDbHelper = new DBHelper(getContext());
db = mDbHelper.getWritableDatabase();
// 初始化两个表的数据(先清空两个表,再各加入一个记录)
db.execsql("delete from user");
db.execsql("insert into user values(1,'Carson');");
db.execsql("insert into user values(2,'Kobe');");
MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo("宝马","奔驰","路虎","宾利","保时捷"));
MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo("宝马1","奔驰2","路虎3","宾利4","保时捷5"));
return true;
}
public static final int ITEMS = 1;
public static final int ITEMS_ID = 2;
// 如果 *** 作的数据属于集合类型,那么MIME类型字符串应该以vnd.androID.cursor.dir/开头。
// 例如:要得到所有person记录的Uri为content://com.cfox.contentprovID.PersonProvIDer/person
// 那么返回的MIME类型字符串应该为:"vnd.androID.cursor.dir/person"
// 如果要 *** 作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.androID.cursor.item/开头
// 例如:得到ID为10的person记录,Uri为content://com.cfox.contentprovID.PersonProvIDer/person/10
// 那么返回的MIME类型字符串为:"vnd.androID.cursor.item/person"
@OverrIDe
public String getType(Uri uri) {
return null;
}
@OverrIDe
public Uri insert(Uri uri, ContentValues values) {
//该方法用于供外部应用往ContentProvIDer添加数据。
// 根据URI匹配 URI_CODE,从而匹配ContentProvIDer中相应的表名
// 该方法在最下面
String table = gettablename(uri);
Log.e("sdfsdf",table+"====="+MyDataBaseHelper.table_USER_AF);
if(table != null && !MyDataBaseHelper.table_USER_AF.equals(table)){
// 向该表添加数据
db.insert(table, null, values);
// 当该URI的ContentProvIDer数据发生变化时,通知外界(即访问该ContentProvIDer数据的访问者)
mContext.getContentResolver().notifyChange(uri, null);
return uri;
}else{
FinnInsert(uri,values);
return uri;
}
// // 通过ContentUris类从URL中获取ID
// long personID = ContentUris.parseID(uri);
// System.out.println(personID);
}
@OverrIDe
public int delete(Uri uri, String selection, String[] selectionArgs) {
//该方法用于供外部应用从ContentProvIDer删除数据。
String tablename = gettablename(uri);
if(!MyDataBaseHelper.table_USER_AF.equals(tablename)){
//使用原生函数的数据库
return 0;
}else{
//使用封装之后的数据库
return 0;
}
}
@OverrIDe
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
/**
* 查询数据
*/
@OverrIDe
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//该方法用于供外部应用从ContentProvIDer中获取数据。
// 根据URI匹配 URI_CODE,从而匹配ContentProvIDer中相应的表名
// 该方法在最下面
String tablename = gettablename(uri);
Finnquery(tablename);
// // 通过ContentUris类从URL中获取ID
// long personID = ContentUris.parseID(uri);
// System.out.println(personID);
// 查询数据+
if(!MyDataBaseHelper.table_USER_AF.equals(tablename)){
//使用原生函数的数据库
return db.query(tablename,projection,selection,selectionArgs,null,null,sortOrder,null);
}else{
//使用封装之后的数据库
return MyDataBase.getInstance(mContext).queryCursor();
}
}
/**
* 根据URI匹配 URI_CODE,从而匹配ContentProvIDer中相应的表名
*/
public static final String gettablename(Uri uri){
String tablename = null;
switch (mMatcher.match(uri)) {
case User_Code:
tablename = DBHelper.USER_table_name;
break;
case Job_Code:
tablename = DBHelper.JOB_table_name;
break;
case AF_Code:
tablename = MyDataBaseHelper.table_USER_AF;
break;
}
return tablename;
}
public voID FinnInsert(Uri uri, ContentValues values) {
//自行封装方法添加数据库
String table = gettablename(uri);
switch (table){//根据表名 插入数据
case MyDataBaseHelper.table_USER_AF://通过KEY获取value
MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo(
values.get("car_user_az_af").toString(), values.get("car_kfb_name_af").toString(),
values.get("car_user_cartype_af").toString(), values.get("car_kfb_date_af").toString(),
values.get("car_kfb_lengmei_af").toString()
));
// MyDataBase.getInstance(mContext).InsertCaraF(new CarInfo(
// values.get(MyDataBaseHelper.USER_CAR_AZ_AF).toString(), values.get(MyDataBaseHelper.USER_CAR_name_AF).toString(),
// values.get(MyDataBaseHelper.USER_CAR_TYPE_AF).toString(), values.get(MyDataBaseHelper.USER_CAR_DATE_AF).toString(),
// values.get(MyDataBaseHelper.USER_CAR_LENGM_AF).toString()
// ));
break;
default:
break;
}
}
public voID Finnquery(String tablename) {
//自行封装方法添加数据库
switch (tablename){//根据表名 插入数据
case MyDataBaseHelper.table_USER_AF:
mListener.OnReList(MyDataBase.getInstance(mContext).queryListAF());
break;
default:
break;
}
}
}
以上是内存溢出为你收集整理的Android 数据存储之ContentProvider 使用和应用场景全部内容,希望文章能够帮你解决Android 数据存储之ContentProvider 使用和应用场景所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)