亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權(quán)限的教程

 更新時(shí)間:2016年04月29日 15:34:49   作者:zhoulc  
這篇文章主要介紹了Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權(quán)限的教程,這樣以來不同的apk就可以互相訪問對(duì)應(yīng)的app文件夾,需要的朋友可以參考下

Android會(huì)為每個(gè)apk進(jìn)程分配一個(gè)單獨(dú)的空間(比如只能訪問/data/data/自己包名下面的文件),一般情況下apk之間是禁止相互訪問數(shù)據(jù)的。通過Shared User id,擁有同一個(gè)User id的多個(gè)APK可以配置成運(yùn)行在同一個(gè)進(jìn)程中.所以默認(rèn)就是可以互相訪問任意數(shù)據(jù). 也可以配置成運(yùn)行成不同的進(jìn)程, 同時(shí)可以訪問其他APK的數(shù)據(jù)目錄下的數(shù)據(jù)庫(kù)和文件.就像訪問本程序的數(shù)據(jù)一樣(使用IPC機(jī)制,不同進(jìn)程之間,比如AIDL)。

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

2016429153254163.png (295×465)

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

2016429153341484.png (492×64)

二、通過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。可以參見Settings等

系統(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??梢詤⒁奊allery等。

四、問題解決

最后還說下,這個(gè)android:sharedUserId屬性不只可以把a(bǔ)pk放到系統(tǒng)進(jìn)程中,也可以配置多個(gè)APK運(yùn)行在一個(gè)進(jìn)程中,這樣可以共享數(shù)據(jù),應(yīng)該會(huì)很有用的

在AndroidMenifest.xml中我們可以看到android:sharedUserId="android.uid.system"
但是有了這句后,就無法對(duì)sd卡進(jìn)行讀寫操作,比如在SD卡中創(chuàng)建一個(gè)新文件夾,是創(chuàng)建不成功的。但是如果把a(bǔ)ndroid:sharedUserId="android.uid.system"注釋掉,就可以在SD卡進(jìn)行IO操作了。
在Settings中android:sharedUserId="android.uid.system"是不可少的,少了它很多Settings下應(yīng)用直接開不了,或一開就報(bào)錯(cuò)。

解決方法一:
  
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卡操作的功能獨(dú)立出去,做成一個(gè)獨(dú)立的APK,然后在原項(xiàng)目中調(diào)用改功能就可以了。
  

相關(guān)文章

最新評(píng)論