溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中如何利用shared_user_id獲取系統(tǒng)權(quán)限

發(fā)布時(shí)間:2022-04-12 14:11:32 來源:億速云 閱讀:479 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“Android中如何利用shared_user_id獲取系統(tǒng)權(quán)限”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android中如何利用shared_user_id獲取系統(tǒng)權(quán)限”吧!

一、使用同一個(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

Android中如何利用shared_user_id獲取系統(tǒng)權(quán)限

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)效果:

Android中如何利用shared_user_id獲取系統(tǒng)權(quá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??梢詤⒁奡ettings等

系統(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。

到此,相信大家對(duì)“Android中如何利用shared_user_id獲取系統(tǒng)權(quán)限”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI