日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android ContentProvider实现两个程序间数据共享demo,跨进程通讯

發(fā)布時(shí)間:2023/12/10 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android ContentProvider实现两个程序间数据共享demo,跨进程通讯 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、客戶端代碼:

先實(shí)現(xiàn)服務(wù)端

SQL創(chuàng)建:

public class DBHelper extends SQLiteOpenHelper {// 數(shù)據(jù)庫名private static final String DATABASE_NAME = "finch.db";// 表名public static final String USER_TABLE_NAME = "user";public static final String JOB_TABLE_NAME = "job";private static final int DATABASE_VERSION = 1;//數(shù)據(jù)庫版本號(hào)public DBHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase db) {// 創(chuàng)建兩個(gè)表格:用戶表 和職業(yè)表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)");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} } ContentProvider類代碼:

?

public class MyProvider extends ContentProvider {private Context mContext;DBHelper mDbHelper = null;SQLiteDatabase db = null;public static final String AUTOHORITY = "scut.carson_ho.myprovider";// 設(shè)置ContentProvider的唯一標(biāo)識(shí)public static final int User_Code = 1;public static final int Job_Code = 2;// UriMatcher類使用:在ContentProvider 中注冊(cè)URIprivate static final UriMatcher mMatcher;static{mMatcher = new UriMatcher(UriMatcher.NO_MATCH);// 初始化mMatcher.addURI(AUTOHORITY,"user", User_Code);mMatcher.addURI(AUTOHORITY, "job", Job_Code);// 若URI資源路徑 = content://cn.scu.myprovider/user ,則返回注冊(cè)碼User_Code// 若URI資源路徑 = content://cn.scu.myprovider/job ,則返回注冊(cè)碼Job_Code}// 以下是ContentProvider的6個(gè)方法/*** 初始化ContentProvider*/@Overridepublic boolean onCreate() {mContext = getContext();// 在ContentProvider創(chuàng)建時(shí)對(duì)數(shù)據(jù)庫進(jìn)行初始化// 運(yùn)行在主線程,故不能做耗時(shí)操作,此處僅作展示mDbHelper = new DBHelper(getContext());db = mDbHelper.getWritableDatabase();// 初始化兩個(gè)表的數(shù)據(jù)(先清空兩個(gè)表,再各加入一個(gè)記錄)db.execSQL("delete from user");db.execSQL("insert into user values(1,'Carson');");db.execSQL("insert into user values(2,'Kobe');");db.execSQL("delete from job");db.execSQL("insert into job values(1,'Android');");db.execSQL("insert into job values(2,'iOS');");return true;}/*** 添加數(shù)據(jù)*/@Overridepublic Uri insert(Uri uri, ContentValues values) {// 根據(jù)URI匹配 URI_CODE,從而匹配ContentProvider中相應(yīng)的表名// 該方法在最下面String table = getTableName(uri);// 向該表添加數(shù)據(jù)db.insert(table, null, values);// 當(dāng)該URI的ContentProvider數(shù)據(jù)發(fā)生變化時(shí),通知外界(即訪問該ContentProvider數(shù)據(jù)的訪問者)mContext.getContentResolver().notifyChange(uri, null);// // 通過ContentUris類從URL中獲取ID // long personid = ContentUris.parseId(uri); // System.out.println(personid);return uri;}/*** 查詢數(shù)據(jù)*/@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {// 根據(jù)URI匹配 URI_CODE,從而匹配ContentProvider中相應(yīng)的表名// 該方法在最下面String table = getTableName(uri);// // 通過ContentUris類從URL中獲取ID // long personid = ContentUris.parseId(uri); // System.out.println(personid);// 查詢數(shù)據(jù)return db.query(table,projection,selection,selectionArgs,null,null,sortOrder,null);}/*** 更新數(shù)據(jù)*/@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {// 由于不展示,此處不作展開return 0;}/*** 刪除數(shù)據(jù)*/@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// 由于不展示,此處不作展開return 0;}@Overridepublic String getType(Uri uri) {// 由于不展示,此處不作展開return null;}/*** 根據(jù)URI匹配 URI_CODE,從而匹配ContentProvider中相應(yīng)的表名*/private 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;}return tableName;}}

服務(wù)端源碼:https://download.csdn.net/download/meixi_android/10698025

?

?

?

客戶端通過uri鏈接到服務(wù)端:

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*** 對(duì)user表進(jìn)行操作*/// 設(shè)置URIUri uri_user = Uri.parse("content://scut.carson_ho.myprovider/user"); // Uri uri_user = Uri.parse("content://cn.scu.myprovider/user");// 插入表中數(shù)據(jù)ContentValues values = new ContentValues();values.put("_id", 4);values.put("name", "Jordan");// 獲取ContentResolverContentResolver resolver = getContentResolver();// 通過ContentResolver 根據(jù)URI 向ContentProvider中插入數(shù)據(jù)resolver.insert(uri_user,values);// 通過ContentResolver 向ContentProvider中查詢數(shù)據(jù)Cursor cursor = resolver.query(uri_user, new String[]{"_id","name"}, null, null, null);while (cursor.moveToNext()){System.out.println("query11111 book:" + cursor.getInt(0) +" "+ cursor.getString(1));// 將表中數(shù)據(jù)全部輸出}cursor.close();// 關(guān)閉游標(biāo)/*** 對(duì)job表進(jìn)行操作*/// 和上述類似,只是URI需要更改,從而匹配不同的URI CODE,從而找到不同的數(shù)據(jù)資源Uri uri_job = Uri.parse("content://scut.carson_ho.myprovider/job"); // Uri uri_job = Uri.parse("content://cn.scu.myprovider/job");// 插入表中數(shù)據(jù)ContentValues values2 = new ContentValues();values2.put("_id", 4);values2.put("job", "NBA Player");// 獲取ContentResolverContentResolver resolver2 = getContentResolver();// 通過ContentResolver 根據(jù)URI 向ContentProvider中插入數(shù)據(jù)resolver2.insert(uri_job,values2);// 通過ContentResolver 向ContentProvider中查詢數(shù)據(jù)Cursor cursor2 = resolver2.query(uri_job, new String[]{"_id","job"}, null, null, null);while (cursor2.moveToNext()){System.out.println("query11111 job:" + cursor2.getInt(0) +" "+ cursor2.getString(1));// 將表中數(shù)據(jù)全部輸出}cursor2.close();// 關(guān)閉游標(biāo)} }

客戶端源碼:https://download.csdn.net/download/meixi_android/10698034

BroadcastReceiver實(shí)現(xiàn)跨進(jìn)程通訊:https://blog.csdn.net/meixi_android/article/details/83615930

總結(jié)

以上是生活随笔為你收集整理的Android ContentProvider实现两个程序间数据共享demo,跨进程通讯的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。