shareUserId介绍:
Android给每个APK进程分配一个单独的空间,manifest中的userid就是对应一个分配的Linux用户ID,并且为它创建一个沙箱,以防止影响其他应用程序(或者其他应用程序影响它)。用户ID 在应用程序安装到设备中时被分配,并且在这个设备中保持它的永久性。
通常,不同的APK会具有不同的userId,因此运行时属于不同的进程中,而不同进程中的资源是不共享的,在保障了程序运行的稳定。然后在有些时候,我们自己开发了多个APK并且需要他们之间互相共享资源,那么就需要通过设置shareUserId来实现这一目的。
通过Shared User id,拥有同一个User id的多个APK可以配置成运行在同一个进程中.所以默认就是可以互相访问任意数据. 也可以配置成运行成不同的进程, 同时可以访问其他APK的数据目录下的数据库和文件.就像访问本程序的数据一样。
APK共享进程要两个条件
(1)设置相同的User Id:(只设置这个还是在两个进程里,只是uid是一样的)
(2)被调用的activity设置以下属性:
<activity android:multiprocess="true"
或者
<activity android:process="com.cienet.test"
shareUserId设置:
在需要共享资源的项目的每个AndroidMainfest.xml中添加shareuserId的标签。
android:sharedUserId="com.example"
id名自由设置,但必须保证每个项目都使用了相同的sharedUserId。一个mainfest只能有一个Shareuserid标签。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.shareusertesta" android:versionCode="1" android:versionName="1.0" android:sharedUserId="com.example">
\data\data\自定义的package\ 路径下的互相访问
每个安装的程序都会根据自己的包名在手机文件系统的data\data\your package\建立一个文件夹(需要su权限才能看见),用于存储程序相关的数据。
在代码中,我们通过context操作一些IO资源时,相关文件都在此路径的相应文件夹中。比如默认不设置外部路径的文件、DB等等。
正常情况下,不同的apk无法互相访问对应的app文件夹。但通过设置相同的shareUserId后,就可以互相访问了。代码如下。
//程序A: public class MainActivityA extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView1); WriteSettings(this, "123"); } public void WriteSettings(Context context, String data) { FileOutputStream fOut = null; OutputStreamWriter osw = null; try { //默认建立在data/data/xxx/file/ fOut = openFileOutput("settings.dat", MODE_PRIVATE); osw = new OutputStreamWriter(fOut); osw.write(data); osw.flush(); Toast.makeText(context, "Settings saved", Toast.LENGTH_SHORT) .show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "Settings not saved", Toast.LENGTH_SHORT) .show(); } finally { try { osw.close(); fOut.close(); } catch (IOException e) { e.printStackTrace(); } } }
//程序B: public class MainActivityB extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) this.findViewById(R.id.textView1); try { //获取程序A的context Context ctx = this.createPackageContext( "com.example.shareusertesta", Context.CONTEXT_IGNORE_SECURITY); String msg = ReadSettings(ctxDealFile); Toast.makeText(this, "DealFile2 Settings read" + msg, Toast.LENGTH_SHORT).show(); WriteSettings(ctx, "deal file2 write"); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String ReadSettings(Context context) { FileInputStream fIn = null; InputStreamReader isr = null; char[] inputBuffer = new char[255]; String data = null; try { //此处调用并没有区别,但context此时是从程序A里面获取的 fIn = context.openFileInput("settings.dat"); isr = new InputStreamReader(fIn); isr.read(inputBuffer); data = new String(inputBuffer); textView.setText(data); Toast.makeText(context, "Settings read", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "Settings not read", Toast.LENGTH_SHORT) .show(); } finally { try { isr.close(); fIn.close(); } catch (IOException e) { e.printStackTrace(); } } return data; } public void WriteSettings(Context context, String data) { FileOutputStream fOut = null; OutputStreamWriter osw = null; try { fOut = context.openFileOutput("settings.dat", MODE_PRIVATE); //此处调用并没有区别,但context此时是从程序A里面获取的 osw = new OutputStreamWriter(fOut); osw.write(data); osw.flush(); Toast.makeText(context, "Settings saved", Toast.LENGTH_SHORT) .show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "Settings not saved", Toast.LENGTH_SHORT) .show(); } finally { try { osw.close(); fOut.close(); } catch (IOException e) { e.printStackTrace(); } } } }
如果A和B的mainfest中设置了相同的shareuserId,那么B的read函数就能正确读取A写入的内容。否则,B无法获取该文件IO。
通过这种方式,两个程序之间不需要代码层级的引用。之间的约束是,B需要知道A的file下面存在“settings.dat”这个文件以及B需要知道A的package的name。