简体   繁体   English

在两个Android应用程序中访问共享的首选项

[英]Accessing shared preferences in two Android Applications

I have two projects with the same signature and the same share user id set. 我有两个具有相同签名和相同共享用户ID集的项目。 I can access shared preferences across the two applications if I explicitly specify the two applications to run on the same process, however if I do not specify the process in the manifest of the two applications, I am not able to access data across applications. 如果我明确指定两个应用程序在同一进程上运行,则可以在两个应用程序之间访问共享首选项,但是,如果我没有在两个应用程序的清单中指定进程,则无法跨应用程序访问数据。 How can I access the shared preferences across the two applications if they run on separate processes? 如果两个应用程序在单独的进程上运行,如何访问它们的共享首选项?

I think its wise the two applications store the data from shared preferences into an sqlite database. 我认为这两个应用程序将共享首选项中的数据存储到sqlite数据库中是明智的。 You then use a contentProvider to share the data across the two apps. 然后,您可以使用contentProvider在两个应用程序之间共享数据。

Forexample apps like facebook, whatsup use the contacts stored in the phone by using the content provider of the contacts app. 例如Facebook之类的应用程序,whatsup通过使用联系人应用程序的内容提供商来使用存储在手机中的联系人。

Read more about content providers http://developer.android.com/guide/topics/providers/content-providers.html 阅读有关内容提供商的更多信息http://developer.android.com/guide/topics/providers/content-providers.html

Okay! 好的! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences. 在应用程序1(程序包名称为“ com.sharedpref1”)中使用此代码存储具有“共享首选项”的数据。

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: 并在应用程序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());
        }

More information please visit this URL: http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html 有关更多信息,请访问以下URL: http : //androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html

From developer.android 来自developer.android
This constant was deprecated in API level 17. Creating world-readable files is very dangerous, and likely to cause security holes in applications. 此常数在API级别17中已弃用。创建世界可读的文件非常危险,并且有可能在应用程序中造成安全漏洞。 It is strongly discouraged; 强烈建议不要这样做。 instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and Service. 相反,应用程序应使用更正式的机制进行交互,例如ContentProvider,BroadcastReceiver和Service。 There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore. 无法保证此访问模式将保留在文件上,例如在进行备份和还原时。 File creation mode: allow all other applications to have read access to the created file. 文件创建模式:允许所有其他应用程序对创建的文件具有读取权限。

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

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