简体   繁体   English

访问另一个应用程序的共享首选项

[英]Accessing shared preferences of another application

I am trying to access the shared preferences of another application using this code: 我正在尝试使用以下代码访问另一个应用程序的共享首选项:

final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
            @SuppressWarnings("deprecation")
            Context myContext = null;
            try {
                myContext = createPackageContext("owning app's pkg name", 
                Context.CONTEXT_IGNORE_SECURITY);
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // where com.example is the owning  app containing the  preferences
              @SuppressWarnings("deprecation")
            SharedPreferences testPrefs = myContext.getSharedPreferences 
            ("file", Context.CONTEXT_IGNORE_SECURITY); 


            Map<String, ?> items = testPrefs .getAll();
            System.out.println(items.size());
            for(String s : items.keySet())
            {
                //do somthing like String value=     items.get(s).toString());
                System.out.println(items.get(s).toString());
                }

However I am getting the size as 0 of the map "items". 但是,我得到的大小为地图“项目”的0。 This is the stack trace: 这是堆栈跟踪:

12-04 12:24:13.659: W/SharedPreferencesImpl(22768): Attempt to read preferences file   /data/data/owning app's pkg name/shared_prefs/filename.xml without permission
12-04 12:24:13.659: I/System.out(22768): 0

It indicates that the owning app doesn't support the sharing. 这表明拥有的应用程序不支持共享。 Is there any way I can overcome this? 有什么办法可以克服这个问题?

owning application needs to have mode WORLD_READABLE permission. 拥有的应用程序需要具有模式WORLD_READABLE权限。 Additionally you have to add same android:sharedUserId in the both app's manifest file. 此外,您必须在两个应用的清单文件中添加相同的android:sharedUserId。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM