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

来源:互联网 发布:淘宝地垫 编辑:程序博客网 时间:2024/05/16 14:30

转:http://blog.csdn.net/wlsfling/article/details/43309427

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<manifestxmlns: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>
                <actionandroid:name="android.intent.action.MAIN"/>
 
                <categoryandroid:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
 
</manifest>

B.apk(实现访问资源并且拷贝)
MainActivity.Java(如何访问assets资源文件请看上一篇http://my.oschina.net/zhoulc/blog/118693)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
packagecom.example.demo2;
 
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
 
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.pm.PackageManager.NameNotFoundException;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.support.v4.app.NavUtils;
 
publicclassMainActivity extendsActivity {
 
    @Override
    publicvoidonCreate(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 = newFile("/data/data/com.example.demo1/session.log");
            if(!file.exists()) {
 
                file.createNewFile();
            }
            input = context.getAssets().open("session.log");
            output = newFileOutputStream(file);
            byte[] buffer = newbyte[1024];
            intreadLength = 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
    publicbooleanonCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        returntrue;
    }
 
}
AndroidManifest.xml 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<manifestxmlns: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>
                <actionandroid:name="android.intent.action.MAIN"/>
 
                <categoryandroid:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
 
</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等。


0 0