xposed 配置如何传入指定模块(ContentProvider)
當我們編寫有界面的xposed模塊時,我們所給用戶提供了可以配置的數據,但是配置的界面是在當前進程,而執行的hook代碼在另外一個進程,那么怎么傳遞這些數據呢?
根據研究有如下三個方法
1.XSharedPreferences(android 7.0以后便不能用了)
2.文件(不確定目標程序是否開啟權限,也不適用)
3.contentprovider (可用)
所以最后就決定了使用contentprovider
Uri
首先我們需要了解Uri,Uri由兩部分組成 authority和path
authority:用來區分不同的程序,一般以包名命名
path:對同一程序中的表的區分
Uri前部需要添加協議說明
例如:content://com.xxx.xxx/user(com.xxx.xxx中的user表)
如果我們希望查詢user表中id為1的數據應該這樣寫
ContentResolver
要想使用contentprovider,就需要借助到contentresolver。通過context.getContentResolver();來獲取該類。其實較為常用的方法有四種 insert,updata,delete,query
query:
Cursor querys = getContentResolver().query(Uri.parse("content://com.xxx.xxx/user"), null, null, null, null);if (querys != null) {while (querys.moveToNext()) {querys.getString(querys.getColumnIndex("packname")));//獲取數據中的packname字段的值}querys.close(); query(uri,projection,selection,selectionArgs,sortorder) uri:獲取到的uri projection:查詢的列名 selection:查詢的條件 例如“packname=?” selecionArgs:查詢條件的值 例如:new String[]{"123"} sortotder:排序的方式insert
ContentValues contentValues = new ContentValues();contentValues.put("packname", pageitens.get(position).packname);context.getContentResolver().insert(parse, contentValues); 我的表設計的簡單 只有兩個字段 一個id(自增) 一個packnameupdate
ContentValues contentValues = new ContentValues();contentValues.put("packname", pageitens.get(position).packname);context.getContentResolver().update(parse, contentValues,“packname=?”,new String[]{"123"}); 這個也比較好理解 update(uri,value,selection,selectinArgs) uri:獲取到的uri value:要修改的數據 selection:查詢的條件 例如“packname=?” selecionArgs:查詢條件的值 例如:new String[]{"123"}delete
context.getContentResolver().delete(parse, "packname=?", new String[]{pageitens.get(position).packname}); delete(uri,selection,selectinArgs) uri:獲取到的uri selection:查詢的條件 例如“packname=?” selecionArgs:查詢條件的值 例如:new String[]{"123"}ContentPeovider
public class TestContentProvider extends ContentProvider {public static final String AUTHO = "com.xxx.xxx";public static final int USER = 1;public static final int USERITEM = 2;public static final int USERITEM1 = 3;private DBHelper dbHelper;private UriMatcher uriMatcher;{//用來篩選uriuriMatcher = new UriMatcher(UriMatcher.NO_MATCH);uriMatcher.addURI(AUTHO, "user", USER);//#表示任意數字 *表示任意長度字符uriMatcher.addURI(AUTHO, "user/#", USERITEM);uriMatcher.addURI(AUTHO, "*", USERITEM1);}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// Implement this to handle requests to delete one or more rows.SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();int deleteint = 0;switch (uriMatcher.match(uri)) {//根據uri篩選表case USER://刪除數據deleteint = writableDatabase.delete("user", selection, selectionArgs);break;}return deleteint;}@Overridepublic String getType(Uri uri) {/*** MIME兩種類型* 1.uri以路徑結尾 前接vnd.android.cursor.dir/vnd.authority.path* 2.uri以id結尾 后接vnd.android.cursor.item/vnd.authority.path*/switch (uriMatcher.match(uri)) {case USER:return "vnd.android.cursor.dir/vnd.com.xxx.xxx/user";case USERITEM:return "vnd.android.cursor.item/vnd.com.xxx.xxx/user";case USERITEM1:return "vnd.android.cursor.item/vnd.com.xxx.xxx/user";default:break;}return null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();Uri inserturi = null;switch (uriMatcher.match(uri)) {//根據uri篩選表case USER://添加數據long user = writableDatabase.insert("user", null, values);inserturi = Uri.parse("content://" + AUTHO + "/user/" + user);break;default:throw new IllegalStateException("Unexpected value: " + uriMatcher.match(uri));}return inserturi;}@Overridepublic boolean onCreate() {//初始化數據庫dbHelper = new DBHelper(getContext());return true;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();Cursor cursor = null;//根據uri篩選表switch (uriMatcher.match(uri)) {case USER:cursor = readableDatabase.query("user", projection, selection, selectionArgs, null, null, sortOrder);break;}return cursor;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();int updatint = 0;switch (uriMatcher.match(uri)) {case USER:updatint = writableDatabase.update("user", values, selection, selectionArgs);break;}return updatint;} }重點講解兩個方法 UriMatcher,gettype,query(增刪改查都差不多)
UriMatcher
getType
MIME兩種類型* 1.uri以路徑結尾 前接vnd.android.cursor.dir/vnd.authority.path* 2.uri以id結尾 后接vnd.android.cursor.item/vnd.authority.path*/代碼中有案例最后記得在AndroidManifest中對剛剛寫的ContentProvider進行注冊 就可以了
<providerandroid:name=".TestContentProvider"android:authorities="com.xxx.xxx"android:enabled="true"android:exported="true" />在hook代碼中通過ContentResolver根據Uri進行數據的操作即可
總結
以上是生活随笔為你收集整理的xposed 配置如何传入指定模块(ContentProvider)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 半导封装测试流程_江苏半导体封装测试服务
- 下一篇: DT时代,国内大数据分析的发展现状是什么