Android:检索其他应用程序的共享首选项

问题描述 投票:30回答:6

我有一个设置应用程序,我必须从中检索其他应用程序首选项,但我没有其中的键的详细信息,如何检索该首选项中的所有可用键和值?

谢谢,斯瓦蒂

android preferences shared
6个回答
24
投票

假设首选项是WORLD_READABLE,这可能有效:

final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
// where com.example is the owning  app containing the preferences
Context myContext = createPackageContext("com.example", Context.MODE_WORLD_WRITEABLE); 
SharedPreferences testPrefs = myContext.getSharedPreferences("test_prefs", Context.MODE_WORLD_READABLE); 
Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()) {
  // do something like String value = items.get(s).toString());
}

48
投票

好的!在应用程序1中使用此代码(包名称为“com.sharedpref1”)以使用共享首选项存储数据。

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

并在Application 2中使用此代码从Application 1中的Shared Preferences获取数据。我们可以得到它,因为我们在应用程序1中使用MODE_WORLD_READABLE:

    try {
                con = createPackageContext("com.sharedpref1", 0);
                SharedPreferences pref = con.getSharedPreferences(
                        "demopref", Context.MODE_PRIVATE);
                String data = pref.getString("demostring", "No Value");
                displaySharedValue.setText(data);

            } catch (NameNotFoundException e) {
                Log.e("Not data shared", e.toString());
            }

更多信息,请访问此URL:http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html


10
投票

此外,您必须在两个应用程序的清单文件中添加相同的android:sharedUserId。


9
投票

不幸的是,文档现在甚至没有解释MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE,而是说:

在API级别17中不推荐使用此常量。创建世界可读文件非常危险,并且可能会在应用程序中造成安全漏洞。强烈劝阻;相反,....等

由于折旧,在具有共享偏好的应用之间实现文件共享可能风险太大,尽管它很简单。我不太关心游戏应用程序中MODE_WORLD_READABLE模式的安全漏洞,我只想将角色从一个应用程序转移到另一个应用程序。他们贬低两种共享模式太糟糕了。


5
投票

如果我们想要来自其他app / pkg / process的读取首选项值,它可以工作。但是jkhouw1的回答有些不对劲:

Context myContext = createPackageContext("com.example", 
            Context.MODE_WORLD_WRITEABLE);

它应该是 :

Context myContext = createPackageContext("com.example", 
            Context.CONTEXT_IGNORE_SECURITY);

虽然,CONTEXT_IGNORE_SECURITY和MODE_WORLD_WRITEABLE具有相同的“int 2”值,完全归功于这个问题和答案。


-1
投票

将一个应用程序的商店共享首选项数据检索到另一个应用程序很简单。

第1步:在两个应用程序的清单文件中添加相同的android:sharedUserId="android.uid.shared"

第2步:存储值application1

 SharedPreferences preferences = context.getSharedPreferences("token_id",    Context.MODE_WORLD_READABLE);
        Editor editor = preferences.edit();
        editor.putString("shared_token", encryptedValue);
        Log.e("aaa *** shared_token : ", encryptedValue.toString());
        editor.commit();

第3步:从application2获取价值

Context con = null;
        try {
            con = createPackageContext("application2 package name", Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        try {
            if (con != null) {
                SharedPreferences pref = con.getSharedPreferences(
                        "token_id", Context.MODE_WORLD_READABLE);

                String data = pref.getString("shared_token", "");
                Log.d("msg", "Other App Data: " + data);
            } else {
                Log.d("msg", "Other App Data: Context null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
© www.soinside.com 2019 - 2024. All rights reserved.