无法从其他应用程序读取SharedPreferences

问题描述 投票:2回答:4

编辑:

我有一个应用程序写入SharedPreferences像这样:

    Context otherAppsContext = null;
    try {
        otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY);
    } catch (NameNotFoundException e) {
    }

    SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("PREFS_PRIVATE", Context.MODE_WORLD_READABLE);
    Editor prefsPrivateEditor = sharedPreferences.edit();
    prefsPrivateEditor.putString("layout02", jString);
    prefsPrivateEditor.putString("layout02name", "Russian Layout");
    prefsPrivateEditor.commit();

和另一个必须从他们读取的应用程序

        Context otherAppsContext = null;
        try {
            otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY);
        } catch (NameNotFoundException e) {
        }

        SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("PREFS_PRIVATE", Context.MODE_WORLD_READABLE);
        Log.e( "name2" , "name2: "+sharedPreferences.getString("layout02name", "") );

但它返回空。

您认为可能是什么问题?

谢谢!

android
4个回答
7
投票

您需要指定应用程序上下文。例如:

Context otherAppsContext = null;
try
{
     otherAppsContext = createPackageContext("<YOUR_PACKAGE_NAME>", Context.MODE_WORLD_READABLE);
}
catch (NameNotFoundException e)
{
}
SharedPreferences prefs = otherAppsContext.getSharedPreferences("PREFS_FILE", Context.MODE_WORLD_READABLE);
String result = prefs.getString("PREFERENCE_TAG_NAME", "DEFAULT_VALUE");

通过这种方式,您可以阅读文件中保存的共享首选项“PREFERENCE_TAG_NAME”的内容

/data/data/YOUR_PACKAGE_NAME/shared_prefs/<PREFS_FILE>.xml

您可以在不同的应用中执行此操作,但为此,“YOUR_PACKAGE_NAME”必须相同。

如果要更改任何应用程序中的值,您需要更改getSharedPreferences的模式:

Context.MODE_WORLD_READABLE

至:

Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE"

2
投票

由于您从另一个包访问SharedPreferences,您需要使用Context.createPackageContext().getSharedPreferences()而不是this.getSharedPreferences()


1
投票

试试这个 :

将SharedPreferences写为:

        Context otherAppsContext = null;
          try {
               otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY);
               } 
catch (NameNotFoundException e) {
               }

myPrefs = otherAppsContext.getSharedPreferences("NAME_OF_SHARED_PREFERENCES", Context.MODE_WORLD_READABLE);
Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("layout02", jString);
prefsEditor.putString("layout02name", "Russian Layout");
prefsEditor.commit();

读SharedPreferences为:

myPrefs = otherAppsContext.getSharedPreferences("NAME_OF_SHARED_PREFERENCES", Context.MODE_WORLD_READABLE);         
String s = myPrefs.getString("layout02name", "") );

0
投票

到目前为止,这是我设法使这项工作的方式。

            String packageName ="com.yourpackage.yourname"
    Context otherAppsContext = null;
        try
        {
            otherAppsContext = createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
        }
        catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "will crash", 3).show();
        }
        SharedPreferences prefsother = otherAppsContext.getSharedPreferences(keyPackageName, Context.MODE_WORLD_READABLE);
        String key = prefsother.getString("key", "defValue");//use prefs normally here
© www.soinside.com 2019 - 2024. All rights reserved.