Android PreferenceActivity to create a MODE_WORLD_WRITEABLE preference across applications

不打扰是莪最后的温柔 提交于 2019-12-22 11:07:44

问题


I have multiple applications that share certain data through preferences. Each app sets its preferences through a PreferenceActitivity (from xml). Two questions:

How do I use/edit preferences created by one app in another app. If I figure out how to create MODE_WORLD_WRITEABLE preferences using PreferenceActivity that will solve the problem.

 SharedPreferences prefs = getSharedPreferences(
          <String referring to another package´s prefs>, MODE_WORLD_WRITEABLE);
        HashMap<String, String> map = (HashMap<String, String>) prefs
          .getAll();


        String str = map.toString();
        tv.setText(str);

Above code returns {}

  • Second, how do I use addPreferencesFromIntent(i) -- I am getting a NullPointerException even though the intent is not Null.

Thanks for the help in advance.

Best, Sameer


回答1:


To access preferences from another app in a secure way set the same android:sharedUserId in the Manifest. This will allow you to access preferences & files in MODE_PRIVATE (or secure) manner.

After much time spent, we realized this alone will not work and one needs to create a package context of the first app to access files in the second app:

try {
            Context c = createPackageContext(com.app.first, MODE_PRIVATE);
            SharedPreferences prefs = c.getSharedPreferences(
                    "com.app.first_preferences", MODE_PRIVATE);

        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

A big thank you to @CommonsWare and Karthik Shanmugam for their help!



来源:https://stackoverflow.com/questions/3536928/android-preferenceactivity-to-create-a-mode-world-writeable-preference-across-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!