gpt4 book ai didi

android - 两个应用程序之间的数据共享

转载 作者:IT老高 更新时间:2023-10-28 21:37:31 25 4
gpt4 key购买 nike

最近我接受了一次采访,其中的问题是“您如何能够在两个已安装的应用程序或 apk 之间共享数据?”

我对这个问题没有任何答案。谁能帮我确定一种方法...

最佳答案

从应用程序 1 发送数据(例如:应用程序 1 包名称为“com.sharedpref1”)。

SharedPreferences prefs = getSharedPreferences("demopref",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();

在应用程序 2 中接收数据(从应用程序 1 的共享首选项中获取数据)。

    try {
con = createPackageContext("com.sharedpref1", 0);//first app package name is "com.sharedpref1"
SharedPreferences pref = con.getSharedPreferences(
"demopref", Context.MODE_PRIVATE);
String your_data = pref.getString("demostring", "No Value");
}
catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}

在两个应用程序 list 文件中添加相同的共享用户 ID 和标签,

 android:sharedUserId="any string" 
android:sharedUserLabel="@string/any_string"

两者相同...且共享用户标签必须来自 string.xml

喜欢这个例子。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxx"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="any string"
android:sharedUserLabel="@string/any_string">

关于android - 两个应用程序之间的数据共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5745243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com