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

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


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

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(实现访问资源而且拷贝)
MainActivity.java(如何访问assets资源文件请看上一篇http://my.oschina.net/zhoulc/blog/118693) session

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
实现效果:

    


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

3、扩展
     app

系统中全部使用android.uid.system做为共享UID的APK,都会首先在manifest节点中增长android:sharedUserId="android.uid.system",而后在Android.mk中增长LOCAL_CERTIFICATE := platform。能够参见Settings等 ide

系统中全部使用android.uid.shared做为共享UID的APK,都会在manifest节点中增长android:sharedUserId="android.uid.shared",而后在Android.mk中增长LOCAL_CERTIFICATE := shared。能够参见Launcher等 ui

系统中全部使用android.media做为共享UID的APK,都会在manifest节点中增长android:sharedUserId="android.media",而后在Android.mk中增长LOCAL_CERTIFICATE := media。能够参见Gallery等。 this

相关文章
相关标签/搜索