无法读取我的其他应用的共享偏好设置

问题描述 投票:0回答:2

我有两个应用程序-App 1和App 2 我想从App1访问App2数据。 但是,我不想要复杂的数据库结构,我只需要从App1读取几个布尔标志。 一种方法是使用内容提供程序,我需要在其中定义表或一些结构化数据。 意图也不是很有用,因为我不想在影响任何更改后立即启动应用程序 任何其他相对不太复杂的解决方法? (我不希望它将它绑定到任何适配器或任何东西。假设在SharedPreference中有两个布尔值:App2中的Value1和Value2,我希望通过App1访问它)

android sharedpreferences android-contentprovider
2个回答
0
投票

第一个应用:

SharedPreferences prefs = getSharedPreferences("test_prefs",
                Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("pref_key", true);
        editor.commit();

第二个应用:

/* where com.example is the first app containing the preferences */
Context myContext = createPackageContext("com.example",Context.CONTEXT_IGNORE_SECURITY); 

SharedPreferences testPrefs = myContext.getSharedPreferences 
    ("test_prefs", Context.MODE_WORLD_READABLE); 


 boolean data = pref.getBoolean("pref_key", false);

0
投票

正确的方法是在App1上创建服务并从App2绑定到此服务。

这是一个供您参考的链接。 https://stuff.mit.edu/afs/sipb/project/android/docs/guide/components/bound-services.html

© www.soinside.com 2019 - 2024. All rights reserved.