不同应用之间共享数据!

平时公司开发的一些软件或者和其他公司合作的一些软件会涉及到应用之间的数据共享,这里提供一种方式,后面还会对另一种方式进行说明。

第一种,通过SharedPreferences 和强大的createPackageContext() 方法可以完全满足要求。但前提条件是要知道

应用的两个东西

1.android:sharedUserId;

2.packageName


两个应用的android:sharedUserId必须是相同的。不然做不到同一进程里面共享数据(这是非常重要的,不然你就可以通过猜别人的包名去做坏事了哟)


两个Demo的布局文件都很简单,不做上传了。主要是两个Demo的java文件

DemoA 写入数据存储在DemoA中的SharePreference里面。

DemoB 去读取DemoA应用中的数据。


DemoA.java


package com.example.evalee.demoa;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DemoA extends AppCompatActivity {
    public static final String SHARE_TFACE = "SHARE_TFFACE_DATA";
    public static final String TFACE = "KEY_TFACE";
    public static final int MAX_LENGTH = 200;
    private EditText editWrite;
    private Button btnFinish;
    private String contentA;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        editWrite = (EditText)findViewById(R.id.write);
        btnFinish = (Button)findViewById(R.id.finish);

        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentA = editWrite.getText().toString();
                if(contentA.length() == 0){
                    Toast.makeText(DemoA.this,"You need to write down something!",Toast.LENGTH_SHORT).show();
                }else if(contentA.length() >0 && contentA.length()<MAX_LENGTH){
                    sharedPreferences = getSharedPreferences(SHARE_TFACE, Context.MODE_PRIVATE);
                    SharedPreferences.Editor prefEditor = sharedPreferences.edit();
                    prefEditor.putString(TFACE,contentA);
                    prefEditor.commit();
                    editWrite.setText("");
                    Toast.makeText(DemoA.this,"Save successful",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

DemoB.java


package com.example.evalee.demob;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DemoB extends AppCompatActivity {
    public static final String SHARE_TFACE = "SHARE_TFFACE_DATA";
    public static final String KEY_TFACE = "KEY_TFACE";
    private TextView textRead;
    private Button btnRead;
    private SharedPreferences sharedPreferences;
    private String showMsg;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textRead = (TextView)findViewById(R.id.showmsg);
        btnRead = (Button)findViewById(R.id.read);
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Context otherAppsContext = null;

                try{
                    otherAppsContext = createPackageContext("com.example.evalee.demoa",Context.CONTEXT_IGNORE_SECURITY);
                }catch (PackageManager.NameNotFoundException e){
                    e.printStackTrace();
                }
                sharedPreferences = otherAppsContext.getSharedPreferences(SHARE_TFACE,Context.MODE_MULTI_PROCESS);
                showMsg = sharedPreferences.getString(KEY_TFACE,"nothing");
                textRead.setText(showMsg);
            }
        });
    }
}

在读取的时候有一个坑。

sharedPreferences = otherAppsContext.getSharedPreferences(SHARE_TFACE,Context.MODE_MULTI_PROCESS);

我在刚开始的时候用的是以下标记去getSharedPreference。结果是数据的写入和读取存在不同步的情况。
Context.CONTEXT_IGNORE_SECURITY


效果图(比较丑,但我没想过去改善)

大家可以跑起来试试。





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值