不同應用之間共享資料!
阿新 • • 發佈:2019-02-14
平時公司開發的一些軟體或者和其他公司合作的一些軟體會涉及到應用之間的資料共享,這裡提供一種方式,後面還會對另一種方式進行說明。
第一種,通過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
效果圖(比較醜,但我沒想過去改善)
大家可以跑起來試試。