Android如何通过shared_user_id获取系统权限

移动开发 Android
在android的API中有提供SystemClock.setCurrentTimeMillis()函数来修改系统时间,可惜无论你怎么调用这个函数都是没用的,无论模拟器还是真机,在logcat中总会得到"Unable to open alarm driver: Permission denied ".这个函数需要root权限或者运行与系统进程中才可以用。

android会为每个apk进程分配一个单独的空间(比如只能访问/data/data/自己包名下面的文件),一般情况下apk之间是禁止相互访问数据的。通 过Shared User id,拥有同一个User id的多个APK可以配置成运行在同一个进程中.所以默认就是可以互相访问任意数据. 也可以配置成运行成不同的进程, 同时可以访问其他APK的数据目录下的数据库和文件.就像访问本程序的数据一样(使用IPC机制,不同进程之间,比如AIDL)。

一、使用同一个shareuserid,多个apk运行到同一个进程,实现多个apk之间的数据访问
    实现效果:把A.apk assets目录下的session.log拷贝到/data/data/A包名/目录下面

A.apk

AndroidManifest.xml

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  2. package="com.example.demo1" 
  3. android:sharedUserId="com.example" 
  4. android:versionCode="1" 
  5. android:versionName="1.0" > 
  6. <uses-sdk 
  7.     android:minSdkVersion="8" 
  8.     android:targetSdkVersion="15" /> 
  9. <application 
  10.     android:icon="@drawable/ic_launcher" 
  11.     android:label="@string/app_name" 
  12.     android:theme="@style/AppTheme" > 
  13.     <activity 
  14.           android:name=".MainActivity" 
  15.           android:label="@string/title_activity_main" > 
  16.           <intent-filter> 
  17.           <action android:name="android.intent.action.MAIN" /> 
  18.                     <category             android:name="android.intent.category.LAUNCHER" /> 
  19.           </intent-filter> 
  20.      </activity> 
  21.      </application> 
  22.      </manifest> 

B.apk(实现访问资源并且拷贝)
MainActivity.java(如何访问assets资源文件请看另一篇http://mobile.51cto.com/aprogram-387591.htm)

  1. package com.example.demo2; 
  2.     import java.io.File; 
  3.     import java.io.FileOutputStream; 
  4.     import java.io.IOException; 
  5.     import java.io.InputStream; 
  6.     import java.io.OutputStream; 
  7.     import android.os.Bundle; 
  8.     import android.app.Activity; 
  9.     import android.content.Context; 
  10.     import android.content.pm.PackageManager.NameNotFoundException; 
  11.     import android.view.Menu; 
  12.     import android.view.MenuItem; 
  13.     import android.support.v4.app.NavUtils; 
  14.     public class MainActivity extends Activity { 
  15.         @Override 
  16.         public void onCreate(Bundle savedInstanceState) { 
  17.             super.onCreate(savedInstanceState); 
  18.             setContentView(R.layout.activity_main); 
  19.             Context context = null
  20.             InputStream input = null
  21.             OutputStream output = null
  22.             try { 
  23.                 context = this.createPackageContext("com.example.demo1"
  24.                         Context.CONTEXT_IGNORE_SECURITY); 
  25.                 File file = new File("/data/data/com.example.demo1/session.log"); 
  26.                 if (!file.exists()) { 
  27.                     file.createNewFile(); 
  28.                 } 
  29.                 input = context.getAssets().open("session.log"); 
  30.                 output = new FileOutputStream(file); 
  31.                 byte[] buffer = new byte[1024]; 
  32.                 int readLength = 0
  33.                 while((readLength = input.read(buffer)) != -1){ 
  34.                     output.write(buffer, 0, readLength); 
  35.                 } 
  36.             } catch (Exception e) { 
  37.                 // TODO Auto-generated catch block 
  38.                 e.printStackTrace(); 
  39.             } 
  40.             finally
  41.                 try { 
  42.                     if(input!=null || output!= null){ 
  43.                         input.close(); 
  44.                         output.close(); 
  45.                         input = null
  46.                         output = null
  47.                     } 
  48.                 } catch (Exception e2) { 
  49.                     // TODO: handle exception 
  50.                 } 
  51.             } 
  52.         } 
  53.       
  54.         @Override 
  55.         public boolean onCreateOptionsMenu(Menu menu) { 
  56.             getMenuInflater().inflate(R.menu.activity_main, menu); 
  57.             return true
  58.         } 
  59.       
  60.     } 

AndroidManifest.xml

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  2.         package="com.example.demo2" 
  3.         android:versionCode="1" 
  4.         android:versionName="1.0" 
  5.         android:sharedUserId="com.example"
  6.         <uses-sdk 
  7.             android:minSdkVersion="8" 
  8.             android:targetSdkVersion="15" /> 
  9.         <application 
  10.             android:icon="@drawable/ic_launcher" 
  11.             android:label="@string/app_name" 
  12.             android:theme="@style/AppTheme" > 
  13.             <activity 
  14.                 android:name=".MainActivity" 
  15.                 android:label="@string/title_activity_main" > 
  16.                 <intent-filter> 
  17.                     <action android:name="android.intent.action.MAIN" /> 
  18.                     <category android:name="android.intent.category.LAUNCHER" /> 
  19.                 </intent-filter> 
  20.             </activity> 
  21.         </application> 
  22.     </manifest> 

A.apk,B.apk使用同一个shareduserid:com.example
实现效果:

二、通过shareduserid来获取系统权限
(1)在AndroidManifest.xml中添加android:sharedUserId="android.uid.system"
(2)在Android.mk文件里面添加LOCAL_CERTIFICATE := platform(使用系统签名)
(3)在源码下面进行mm编译
这样生成的apk能够获取system权限,可以在任意system权限目录下面进行目录或者文件的创建,其他apk资源的访问等(注意创建的文件(夹)只有创建者(比如system,root除外)拥有可读可写权限-rw-------)。

三、扩展

系统中所有使用android.uid.system作为共享UID的APK,都会首先在manifest节点中增加 android:sharedUserId="android.uid.system",然后在Android.mk中增加 LOCAL_CERTIFICATE := platform。可以参见Settings等

系统中所有使用android.uid.shared作为共享UID的APK,都会在manifest节点中增加 android:sharedUserId="android.uid.shared",然后在Android.mk中增加 LOCAL_CERTIFICATE := shared。可以参见Launcher等

系统中所有使用android.media作为共享UID的APK,都会在manifest节点中增加 android:sharedUserId="android.media",然后在Android.mk中增加LOCAL_CERTIFICATE := media。可以参见Gallery等。

责任编辑:闫佳明 来源: oschina
相关推荐

2015-03-09 10:22:23

2010-01-29 09:08:57

Windows 7系统权限

2020-10-12 09:46:34

漏洞

2010-09-27 15:24:11

SQL Server

2009-02-20 12:06:04

Vista完全控制权限安全性

2013-11-01 11:32:49

2013-08-29 15:24:36

2010-10-08 10:27:30

Mysql User表

2010-11-23 13:36:29

Mysql User表

2012-11-23 14:27:43

IBMdW

2017-08-07 17:54:08

Windows 7WindowsTrustedInst

2013-07-11 09:51:15

2010-08-12 13:11:54

Universal AAndroid开发

2018-06-22 10:18:52

2018-06-19 08:12:25

2011-08-02 15:07:43

组策略群集用户账户

2022-04-20 11:27:05

GitHub API

2010-10-26 10:31:11

2011-04-18 14:12:44

2009-03-14 16:03:50

AccessMSSQL漏洞
点赞
收藏

51CTO技术栈公众号