平时公司开发的一些软件或者和其他公司合作的一些软件会涉及到应用之间的数据共享,这里提供一种方式,后面还会对另一种方式进行说明。
第一种,通过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
效果图(比较丑,但我没想过去改善)
大家可以跑起来试试。