部分内容来自网友博客:http://blog.csdn.net/u012481172/article/details/51095658

 

PIDUID存在的意义

        Pid是进程IDUid是用户ID,只是Android和计算机不一样,计算机每个用户都具有一个Uid,哪个用户start的程序,这个程序的Uid就是那个用户,而Android中每个程序都有一个Uid,默认情况下,Android会给每个程序分配一个普通级别互不相同的Uid,如果应用之间要互相调用,只能是Uid相同才行,这就使得共享数据具有了一定安全性,每个软件之间是不能随意获得数据的。而同一个application只有一个Uid,所以application下的Activity之间不存在访问权限的问题。

 

如何实现两个应用uid共享

    首先需要在两个应用的AndroidManifest.xml中都定义相同的sharedUserId,如:android:sharedUserId="com.test";

定义后可以通过adb shell cat data/system/packages.list(限于enguserdebug) 进行确认两个应用的uid是否相同了,

输出结果似如:

com.example.testdatabase 10124 1 /data/data/com.example.testdatabase default nonecom.example.test_6_3_database 10124 1 /data/data/com.example.test_6_3_database default none

证明uid已经是一样了。

 

uid共享res资源实例

    假设我们有这样一个需求,AB是两个应用,现在要求在A中获取B的一张名字为send_bg的图片资源,那么先AB的注册文件的AndroidManifest.xml节点添加sharedUserId,并且赋值相同,然后在A中可以用如下方式实现:

Context thdContext = null;try {    thdContext = createPackageContext(            "com.example.testdatabase",            Context.CONTEXT_IGNORE_SECURITY);    Resources res = thdContext.getResources();      int menuIconId = res.getIdentifier("send_bg", "drawable",              "com.example.testdatabase");      Drawable drawable = res.getDrawable(menuIconId);      mButton.setBackgroundDrawable(drawable);} catch (NameNotFoundException e) {    e.printStackTrace();}

uid共享database实例

          假设我们有这样一个需求,AB是两个应用,现在要求在A获取B数据库,那么先AB的注册文件的AndroidManifest.xml节点添加sharedUserId,并且赋值相同,然后在A中可以用如下方式实现:

Context thdContext = null;try {    thdContext = createPackageContext(            "com.example.testdatabase",            Context.CONTEXT_IGNORE_SECURITY);    String dbPath = thdContext.getDatabasePath("BookStore.db")            .getAbsolutePath();    SQLiteDatabase db = SQLiteDatabase.openDatabase(dbPath,            null, SQLiteDatabase.OPEN_READWRITE);    Cursor cursor = db.query("Book", null, null, null, null,            null, null);    if (cursor.moveToFirst()) {        do {            String name = cursor.getString(cursor                    .getColumnIndex("name"));            Log.d(TAG, "name: " + name);        } while (cursor.moveToNext());    }} catch (NameNotFoundException e) {    e.printStackTrace();}

我们还可以通过uid共享其他资源或者Preference等等。


更多相关文章

  1. android学习指南(2)-应用程序的基本要素
  2. API Guides - introduction
  3. BlueStacks App Player:在PC上运行Android
  4. 利用HTML5开发Android笔记(下篇)
  5. 应用phprpc协议实现Android客户端的一些总结
  6. android:exported 属性详解
  7. Android应用程序进程启动过程的源代码分析
  8. Android核心功能模块介绍
  9. android应用程序基本原理

随机推荐

  1. Android控件笔记——CheckBox复选框
  2. android GridView android:stretchMode="
  3. Android开发神贴整理
  4. Android Manifest merger failed :tools:r
  5. android 编译
  6. Android中的Service 与 Thread 的区别
  7. Android系统中自带的图标
  8. Android编译过程详解(二)
  9. Android程序员必备精品资源
  10. Android运行异常:Unable to start activit