国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

不同應用之間共享數據!

2019-11-09 15:47:09
字體:
來源:轉載
供稿:網友

平時公司開發的一些軟件或者和其他公司合作的一些軟件會涉及到應用之間的數據共享,這里提供一種方式,后面還會對另一種方式進行說明。

第一種,通過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

效果圖(比較丑,但我沒想過去改善)

大家可以跑起來試試。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 交城县| 锡林郭勒盟| 海林市| 仙居县| 临江市| 安泽县| 阿勒泰市| 上林县| 蒙城县| 门头沟区| 大化| 浠水县| 穆棱市| 湖北省| 固始县| 临高县| 武宣县| 汉沽区| 新乐市| 论坛| 彭阳县| 湾仔区| 东源县| 潮安县| 金昌市| 沧州市| 工布江达县| 丹东市| 永平县| 广丰县| 陕西省| 察隅县| 抚州市| 治多县| 扶沟县| 长岛县| 珲春市| 右玉县| 遵义县| 永城市| 德化县|