使用ShareUserId

转载请注明出处: http://blog.csdn.net/a992036795/article/details/51595095

一、Android系统回为每个应用分配一个唯一的UID,具有相同的UID的应用才能共享数据。两个应用通过ShareUID共享数据,需要相同的ShareUID之外还需要相同的签名才可以。在这种情况下他们可以互相访问私有数据,比如data目录,组件信息等。如果他们跑在同一个进程中,那么他们除了能共享data目录,组件信息,还可以共享内存数据。
二、使用
我们来看一下如何使用ShareUserId来共享数据。
1、我们新建创建2个app项目,第一个清单文件如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blueberry.test06"
    android:sharedUserId="com.blueberry"
    >

第二个清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blueberry.app2"
    android:sharedUserId="com.blueberry"
    >

这里,我们测试一下能够互相访问SharePrefences
第一个应用主要代码:

public class MainActivity extends AppCompatActivity {

    private EditText et ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.et);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("sp", MODE_PRIVATE);
                sp.edit().putString("key",et.getText().toString()).commit() ;
            }
        });

    }
}

第二个应用的主要代码:

public class MainActivity extends AppCompatActivity {

    private TextView tv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                  Context otherAppContext =  createPackageContext("com.blueberry.test06", Context.CONTEXT_IGNORE_SECURITY);
                  SharedPreferences sp =  otherAppContext.getSharedPreferences("sp",MODE_PRIVATE);
                  String text =  sp.getString("key","empty");
                    tv.setText(text);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });

    }

}

这里看到一个重要的方法createPackageContext()他实际上是一个Context的方法,他的大体功能应该是通过所给的包名参数,拿到对应应用的context,进而可以访问那个应用的私有资源。
这儿方法的声明如下:


    /**
     * Return a new Context object for the given application name.  This
     * Context is the same as what the named application gets when it is
     * launched, containing the same resources and class loader.  Each call to
     * this method returns a new instance of a Context object; Context objects
     * are not shared, however they share common state (Resources, ClassLoader,
     * etc) so the Context instance itself is fairly lightweight.
     *
     * <p>Throws {@link android.content.pm.PackageManager.NameNotFoundException} if there is no
     * application with the given package name.
     *
     * <p>Throws {@link java.lang.SecurityException} if the Context requested
     * can not be loaded into the caller's process for security reasons (see
     * {@link #CONTEXT_INCLUDE_CODE} for more information}.
     *
     * @param packageName Name of the application's package.
     * @param flags Option flags, one of {@link #CONTEXT_INCLUDE_CODE}
     *              or {@link #CONTEXT_IGNORE_SECURITY}.
     *
     * @return A {@link Context} for the application.
     *
     * @throws SecurityException &nbsp;
     * @throws PackageManager.NameNotFoundException if there is no application with
     * the given package name.
     */
    public abstract Context createPackageContext(String packageName,
            @CreatePackageOptions int flags) throws PackageManager.NameNotFoundException;

最终,我测试成功了,这里就不贴测试结果了。当然除了可以互相访问SharePrefences还可访问data下的文件、数据库等。主要采用方法:
Context的openOrCreateDatabase()、openFileInput()、openFileOutput()方法。
原理都是一样,这里我就不一一测试了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值