Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權(quán)限的教程
Android會為每個apk進程分配一個單獨的空間(比如只能訪問/data/data/自己包名下面的文件),一般情況下apk之間是禁止相互訪問數(shù)據(jù)的。通過Shared User id,擁有同一個User id的多個APK可以配置成運行在同一個進程中.所以默認(rèn)就是可以互相訪問任意數(shù)據(jù). 也可以配置成運行成不同的進程, 同時可以訪問其他APK的數(shù)據(jù)目錄下的數(shù)據(jù)庫和文件.就像訪問本程序的數(shù)據(jù)一樣(使用IPC機制,不同進程之間,比如AIDL)。
一、使用同一個shareuserid,多個apk運行到同一個進程,實現(xiàn)多個apk之間的數(shù)據(jù)訪問
實現(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(實現(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使用同一個shareduserid:com.example
實現(xiàn)效果:
二、通過shareduserid來獲取系統(tǒng)權(quán)限
(1)在AndroidManifest.xml中添加android:sharedUserId="android.uid.system"
(2)在Android.mk文件里面添加LOCAL_CERTIFICATE := platform(使用系統(tǒng)簽名)
(3)在源碼下面進行mm編譯
這樣生成的apk能夠獲取system權(quán)限,可以在任意system權(quán)限目錄下面進行目錄或者文件的創(chuàng)建,以及訪問其他apk資源等(注意創(chuàng)建的文件(夾)只有創(chuàng)建者(比如system,root除外)擁有可讀可寫權(quán)限-rw-------)。
三、擴展
系統(tǒng)中所有使用android.uid.system作為共享UID的APK,都會首先在manifest節(jié)點中增加android:sharedUserId="android.uid.system",然后在Android.mk中增加LOCAL_CERTIFICATE := platform。可以參見Settings等
系統(tǒng)中所有使用android.uid.shared作為共享UID的APK,都會在manifest節(jié)點中增加android:sharedUserId="android.uid.shared",然后在Android.mk中增加LOCAL_CERTIFICATE := shared??梢詤⒁奓auncher等
系統(tǒng)中所有使用android.media作為共享UID的APK,都會在manifest節(jié)點中增加android:sharedUserId="android.media",然后在Android.mk中增加LOCAL_CERTIFICATE := media??梢詤⒁奊allery等。
四、問題解決
最后還說下,這個android:sharedUserId屬性不只可以把apk放到系統(tǒng)進程中,也可以配置多個APK運行在一個進程中,這樣可以共享數(shù)據(jù),應(yīng)該會很有用的
在AndroidMenifest.xml中我們可以看到android:sharedUserId="android.uid.system"
但是有了這句后,就無法對sd卡進行讀寫操作,比如在SD卡中創(chuàng)建一個新文件夾,是創(chuàng)建不成功的。但是如果把android:sharedUserId="android.uid.system"注釋掉,就可以在SD卡進行IO操作了。
在Settings中android:sharedUserId="android.uid.system"是不可少的,少了它很多Settings下應(yīng)用直接開不了,或一開就報錯。
解決方法一:
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卡操作的功能獨立出去,做成一個獨立的APK,然后在原項目中調(diào)用改功能就可以了。
- android listview優(yōu)化幾種寫法詳細(xì)介紹
- Android 動畫之ScaleAnimation應(yīng)用詳解
- android Handler詳細(xì)使用方法實例
- android調(diào)試工具DDMS的使用詳解
- Android的Activity跳轉(zhuǎn)動畫各種效果整理
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- Android按鈕單擊事件的四種常用寫法總結(jié)
- Android 程序申請權(quán)限注意事項
- Android 訪問文件權(quán)限的四種模式介紹
- Android獲取設(shè)備隱私 忽略6.0權(quán)限管理
- Android 6.0權(quán)限申請詳解及權(quán)限資料整理
相關(guān)文章
Android編程實現(xiàn)webview將網(wǎng)頁打包成apk的方法
這篇文章主要介紹了Android編程實現(xiàn)webview將網(wǎng)頁打包成apk的方法,以打包HTML5為例分析了webview打包apk的相關(guān)方法、屬性與事件操作技巧,需要的朋友可以參考下2017-08-08Android實現(xiàn)隨機圓形云標(biāo)簽效果
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)隨機圓形云標(biāo)簽效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android自定義控件之繼承ViewGroup創(chuàng)建新容器
這篇文章主要介紹了Android自定義控件之繼承ViewGroup創(chuàng)建新容器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android 新聞界面模擬ListView和ViewPager的應(yīng)用
本文主要介紹 Android ListView和ViewPager的應(yīng)用,這里模擬了新聞界面及實現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09phonegap教程使用jspdf庫在應(yīng)用中生成pdf文件(pdf生成方法)
在PhoneGap應(yīng)用中生成pdf文件,實現(xiàn)起來很簡單,使用JSPDF這個標(biāo)準(zhǔn)的JavaScript類庫來實現(xiàn)這個功能2014-01-01