Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在两个应用程序之间使用SharedReference?_Android_Android Studio_Sharedpreferences - Fatal编程技术网

Android 如何在两个应用程序之间使用SharedReference?

Android 如何在两个应用程序之间使用SharedReference?,android,android-studio,sharedpreferences,Android,Android Studio,Sharedpreferences,X和Y两个应用程序。它们不是彼此的子类。我想用SharedReferences将一个字符串从X应用程序写入Y,然后从Y应用程序读取它 在第一个应用程序中创建共享首选数据将其模式设置为mode\u WORLD\u READABLE SharedPreferences mSharedPrefs = getSharedPreferences("Prefs_First", MODE_WORLD_READABLE); SharedPreferences.Editor editor = mSharedPre

X和Y两个应用程序。它们不是彼此的子类。我想用SharedReferences将一个字符串从X应用程序写入Y,然后从Y应用程序读取它

在第一个应用程序中创建共享首选数据将其模式设置为
mode\u WORLD\u READABLE

SharedPreferences mSharedPrefs = getSharedPreferences("Prefs_First", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putString("name", etName.getEditableText().toString());
editor.putString("password", etPassword.getEditableText().toString());
editor.commit();
要从其他应用程序访问该数据,请尝试以下操作:

Context mContext = createPackageContext("com.sample.globalsharedpreference", CONTEXT_IGNORE_SECURITY);

SharedPreferences firstAppSharedPrefs = mContext.getSharedPreferences("Prefs_First", Context.MODE_WORLD_READABLE);

String strName = firstAppSharedPrefs.getString("name", "");
String strPassword = firstAppSharedPrefs.getString("password", "");

请注意,
com.sample.globalsharedpreference
是第一个应用程序的包名。

这是否回答了您的问题?你真的是指申请吗?还是活动?由于您提到了子类..注意:自API级别17以来,MODE_WORLD_可读和MODE_WORLD_可写模式已被弃用。从Android 7.0(API级别24)开始,如果您使用它们,Android会抛出SecurityException。如果你的应用程序需要与其他应用程序共享私人文件,它可能会使用带有“授予\读取\ URI\权限”标志的文件提供程序。有关更多信息,请参见共享文件。是2应用程序@RobCoWhat如果我想在发布应用程序后更改它呢?java.lang.SecurityException:MODE\u WORLD\u READABLE不再受支持
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" 
android:sharedUserId="com.example">
myContext = context.createPackageContext(
                        "com.example.secondapp",
                        Context.MODE_PRIVATE);
 shared= myContext.getSharedPreferences("fileName", Activity.MODE_PRIVATE);