Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權(quán)限的教程
Android會(huì)為每個(gè)apk進(jìn)程分配一個(gè)單獨(dú)的空間(比如只能訪問/data/data/自己包名下面的文件),一般情況下apk之間是禁止相互訪問數(shù)據(jù)的。通過Shared User id,擁有同一個(gè)User id的多個(gè)APK可以配置成運(yùn)行在同一個(gè)進(jìn)程中.所以默認(rèn)就是可以互相訪問任意數(shù)據(jù). 也可以配置成運(yùn)行成不同的進(jìn)程, 同時(shí)可以訪問其他APK的數(shù)據(jù)目錄下的數(shù)據(jù)庫(kù)和文件.就像訪問本程序的數(shù)據(jù)一樣(使用IPC機(jī)制,不同進(jìn)程之間,比如AIDL)。
一、使用同一個(gè)shareuserid,多個(gè)apk運(yùn)行到同一個(gè)進(jìn)程,實(shí)現(xiàn)多個(gè)apk之間的數(shù)據(jù)訪問
實(shí)現(xiàn)效果:把A.apk assets目錄下的session.log拷貝到/data/data/A包名/目錄下面
A.apk
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo1" android:sharedUserId="com.example" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
B.apk(實(shí)現(xiàn)訪問資源并且拷貝)
MainActivity.java
package com.example.demo2; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Context context = null; InputStream input = null; OutputStream output = null; try { context = this.createPackageContext("com.example.demo1", Context.CONTEXT_IGNORE_SECURITY); File file = new File("/data/data/com.example.demo1/session.log"); if (!file.exists()) { file.createNewFile(); } input = context.getAssets().open("session.log"); output = new FileOutputStream(file); byte[] buffer = new byte[1024]; int readLength = 0; while((readLength = input.read(buffer)) != -1){ output.write(buffer, 0, readLength); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { if(input!=null || output!= null){ input.close(); output.close(); input = null; output = null; } } catch (Exception e2) { // TODO: handle exception } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo2" android:versionCode="1" android:versionName="1.0" android:sharedUserId="com.example"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
A.apk,B.apk使用同一個(gè)shareduserid:com.example
實(shí)現(xiàn)效果:
二、通過shareduserid來獲取系統(tǒng)權(quán)限
(1)在AndroidManifest.xml中添加android:sharedUserId="android.uid.system"
(2)在Android.mk文件里面添加LOCAL_CERTIFICATE := platform(使用系統(tǒng)簽名)
(3)在源碼下面進(jìn)行mm編譯
這樣生成的apk能夠獲取system權(quán)限,可以在任意system權(quán)限目錄下面進(jìn)行目錄或者文件的創(chuàng)建,以及訪問其他apk資源等(注意創(chuàng)建的文件(夾)只有創(chuàng)建者(比如system,root除外)擁有可讀可寫權(quán)限-rw-------)。
三、擴(kuò)展
系統(tǒng)中所有使用android.uid.system作為共享UID的APK,都會(huì)首先在manifest節(jié)點(diǎn)中增加android:sharedUserId="android.uid.system",然后在Android.mk中增加LOCAL_CERTIFICATE := platform。可以參見Settings等
系統(tǒng)中所有使用android.uid.shared作為共享UID的APK,都會(huì)在manifest節(jié)點(diǎn)中增加android:sharedUserId="android.uid.shared",然后在Android.mk中增加LOCAL_CERTIFICATE := shared??梢詤⒁奓auncher等
系統(tǒng)中所有使用android.media作為共享UID的APK,都會(huì)在manifest節(jié)點(diǎn)中增加android:sharedUserId="android.media",然后在Android.mk中增加LOCAL_CERTIFICATE := media??梢詤⒁奊allery等。
四、問題解決
最后還說下,這個(gè)android:sharedUserId屬性不只可以把a(bǔ)pk放到系統(tǒng)進(jìn)程中,也可以配置多個(gè)APK運(yùn)行在一個(gè)進(jìn)程中,這樣可以共享數(shù)據(jù),應(yīng)該會(huì)很有用的
在AndroidMenifest.xml中我們可以看到android:sharedUserId="android.uid.system"
但是有了這句后,就無法對(duì)sd卡進(jìn)行讀寫操作,比如在SD卡中創(chuàng)建一個(gè)新文件夾,是創(chuàng)建不成功的。但是如果把a(bǔ)ndroid:sharedUserId="android.uid.system"注釋掉,就可以在SD卡進(jìn)行IO操作了。
在Settings中android:sharedUserId="android.uid.system"是不可少的,少了它很多Settings下應(yīng)用直接開不了,或一開就報(bào)錯(cuò)。
解決方法一:
vold 模塊里的 Volume.cpp文件
在調(diào)用doMount的語句里做一下修改~
doMount(devicePath, path, false, false, false,1000, 1015, 0702, true) ↓ doMount(devicePath, path, false, true, false,1000, 1015, 0002, true)
編譯以后試試
解決方法二:
把SD卡操作的功能獨(dú)立出去,做成一個(gè)獨(dú)立的APK,然后在原項(xiàng)目中調(diào)用改功能就可以了。
- android listview優(yōu)化幾種寫法詳細(xì)介紹
- Android 動(dòng)畫之ScaleAnimation應(yīng)用詳解
- android Handler詳細(xì)使用方法實(shí)例
- android調(diào)試工具DDMS的使用詳解
- Android的Activity跳轉(zhuǎn)動(dòng)畫各種效果整理
- Android SQLite數(shù)據(jù)庫(kù)增刪改查操作的使用詳解
- Android按鈕單擊事件的四種常用寫法總結(jié)
- Android 程序申請(qǐng)權(quán)限注意事項(xiàng)
- Android 訪問文件權(quán)限的四種模式介紹
- Android獲取設(shè)備隱私 忽略6.0權(quán)限管理
- Android 6.0權(quán)限申請(qǐng)?jiān)斀饧皺?quán)限資料整理
相關(guān)文章
Android編程實(shí)現(xiàn)webview將網(wǎng)頁(yè)打包成apk的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview將網(wǎng)頁(yè)打包成apk的方法,以打包HTML5為例分析了webview打包apk的相關(guān)方法、屬性與事件操作技巧,需要的朋友可以參考下2017-08-08Android實(shí)現(xiàn)隨機(jī)圓形云標(biāo)簽效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)隨機(jī)圓形云標(biāo)簽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android自定義控件之繼承ViewGroup創(chuàng)建新容器
這篇文章主要介紹了Android自定義控件之繼承ViewGroup創(chuàng)建新容器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android自定義橫向滑動(dòng)菜單的實(shí)現(xiàn)
這篇文章主要介紹了Android自定義橫向滑動(dòng)菜單的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Android 新聞界面模擬ListView和ViewPager的應(yīng)用
本文主要介紹 Android ListView和ViewPager的應(yīng)用,這里模擬了新聞界面及實(shí)現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09Android自定義視圖實(shí)現(xiàn)手指移動(dòng)軌跡
這篇文章主要為大家詳細(xì)介紹了Android自定義視圖實(shí)現(xiàn)手指移動(dòng)軌跡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06phonegap教程使用jspdf庫(kù)在應(yīng)用中生成pdf文件(pdf生成方法)
在PhoneGap應(yīng)用中生成pdf文件,實(shí)現(xiàn)起來很簡(jiǎn)單,使用JSPDF這個(gè)標(biāo)準(zhǔn)的JavaScript類庫(kù)來實(shí)現(xiàn)這個(gè)功能2014-01-01