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

发布于 2024-11-07 17:43:42 字数 83 浏览 0 评论 0原文

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

谢谢, 斯瓦蒂

I have a settings application from which i have to retrieve other applications preferences, but i don't have the details of keys in them, how can i retrieve all the available keys and values in that preference?

Thanks,
Swathi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

沉睡月亮 2024-11-14 17:43:43

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

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

在应用程序2中使用此代码从应用程序1中的共享首选项中获取数据。我们可以获取它,因为我们在应用程序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

Okay! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences.

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

And using this code in Application 2 to get data from Shared Preferences in Application 1. We can get it because we use MODE_WORLD_READABLE in application 1:

    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());
            }

More information please visit this URL:
http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html

半世蒼涼 2024-11-14 17:43:43

假设首选项是 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());
}

Assuming the preference are WORLD_READABLE, this might work:

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());
}
深府石板幽径 2024-11-14 17:43:43

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

Additionally you have to add same android:sharedUserId in the both app's manifest file.

东京女 2024-11-14 17:43:43

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

这个常量在 API 级别 17 中已被贬值。
创建世界可读的文件是非常危险的,并且可能会导致应用程序中的安全漏洞。强烈劝阻;相反,....etc

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

Unfortunately the docs now don't even explain MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE, instead saying:

This constant was depreciated in API level 17.
Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, ....etc

Since the depreciation, implementing file sharing between apps with sharedpreferences may be too risky, although it was simple. I'm not too concerned with security holes from the MODE_WORLD_READABLE mode in game apps where I just want to be able to transfer characters from one app to another. It's too bad they depreciated both sharing modes.

无畏 2024-11-14 17:43:43

如果我们想从其他应用程序/pkg/Process读取peacorpely值,它可以工作。
但是Jkhouw1的答案中有问题:

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

不过:

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

但是,Context_ignore_security和mode_world_writ_writships具有相同的“ int 2”的值
完全感谢这个问题和答案。

It can work if we want read perference value from other app/pkg/process.
but there is something wrong in jkhouw1's answer:

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

It should be :

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

though , CONTEXT_IGNORE_SECURITY and MODE_WORLD_WRITEABLE with the same value of "int 2"
At all ,thanks for this question and answers.

快乐很简单 2024-11-14 17:43:43

continue

It's simple to retrieve store shared preferences data of one application to another application.

Step 1: add the same android:sharedUserId="android.uid.shared" in both app's manifest files.

Step 2: Store Value 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();

Step 3: Get Value From 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();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文