不同进程中的 OnPreferenceChangeListener - 共享偏好数据

发布于 2024-12-13 15:05:49 字数 344 浏览 1 评论 0原文

所以我的问题如下:我有两个服务在不同的进程中运行,并且希望保持这种方式。一种是将数据从数据库总线传输到绑定应用程序,第二种是通过套接字轮询传入数据。我觉得将这些保留在独立的过程中会更好。问题是我希望在两个服务之间有一个共享的首选项,并希望实现 OnSharedPreferenceChangeListener 来更新轮询和总线数据所需的设置。我无法在服务中实现 OnSharedPreferenceChangeListener 因为它们在不同的进程上运行。我可以在 PreferenceActivity 上实现此功能,但如何与服务通信以立即更新?我不想使用 AIDL 并担心绑定。可以创建广播接收器并发送意图,但如果设置菜单增加,这些似乎是一项艰巨的工作。还有其他好主意吗?

So my problem is as follows: I have 2 services running in different processes and would like to keep it this way. One is busing data from databases to bound applications and the second is polling for incoming data through sockets. I feel keeping these in independent process would be better. The problem is that I would like to have a shared preferences between the two services and would like to implement OnSharedPreferenceChangeListener to update setting needed for polling and busing data. I can't implement OnSharedPreferenceChangeListener in the services since they run on different processes. I could implement this on the PreferenceActivity but how do I communicate to the services for immediate update? I do not want to use AIDL and worry about binding. There is the possibility of creating broadcast receivers and sending out intents but these seems like a big work around if the settings menu grows. Any other great ideas out there?

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

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

发布评论

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

评论(2

殊姿 2024-12-20 15:05:49

好吧,这就是您的答案...

对于此示例的首选项,让我们采用 3 个类 - 2 个服务服务 A 和 B(href A,B)和设置(preferenceActivity)

将这两个服务初始化为

public class ServiceA/B extends serice implements OnSharedPreferenceChangeListener{
    @Overside
    public void onCreate(....){
        Settings.getPrefs(this).registerOnSharedPreferenceChangeListener(this);  

     }
    @Override
    protected void onResume() {
        super.onResume();
        Settings.getPrefs(this).registerOnSharedPreferenceChangeListener(this);  
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        Settings.getPrefs(this)
                .unregisterOnSharedPreferenceChangeListener(this);
    }
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    System.out.println("++"+key+"++");
    if(key == "KEYA"||key == "KEYC")
        Do_what_ever_you_want();
            if (key == "KEYB")
        do_anything();
    }
    do_anything(){}
    Do_what_ever_you_want();
}

共享首选项部分。

public class Settings extends PreferenceActivity implements
OnSharedPreferenceChangeListener{
      public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
      public static final String MASTERKEY = "!@#$%^&*";

      public static final String KEYA = "KEYA";
      public static final String KEYB = "KEYB";
      public static final String KEYC = "KEYC";

       --- the create and get methods for getting and sharing data in the prefs... ..... 
      // get them from just a google search.

}

我在我的一个应用程序中实现了这个系统...并部署了...所以摆弄这些基础知识并让我知道它是如何进行的...

Rajesh...

all right here is your answer...

for the preference of this example lets take 3 classes - 2 services service A and B (href A,B) and Settings(preferenceActivity)

initialize the two services as

public class ServiceA/B extends serice implements OnSharedPreferenceChangeListener{
    @Overside
    public void onCreate(....){
        Settings.getPrefs(this).registerOnSharedPreferenceChangeListener(this);  

     }
    @Override
    protected void onResume() {
        super.onResume();
        Settings.getPrefs(this).registerOnSharedPreferenceChangeListener(this);  
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        Settings.getPrefs(this)
                .unregisterOnSharedPreferenceChangeListener(this);
    }
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    System.out.println("++"+key+"++");
    if(key == "KEYA"||key == "KEYC")
        Do_what_ever_you_want();
            if (key == "KEYB")
        do_anything();
    }
    do_anything(){}
    Do_what_ever_you_want();
}

Shared preference Part.

public class Settings extends PreferenceActivity implements
OnSharedPreferenceChangeListener{
      public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
      public static final String MASTERKEY = "!@#$%^&*";

      public static final String KEYA = "KEYA";
      public static final String KEYB = "KEYB";
      public static final String KEYC = "KEYC";

       --- the create and get methods for getting and sharing data in the prefs... ..... 
      // get them from just a google search.

}

I have this system implemented in one of my applicaiton... and deployed... so fiddle around these basics and let me know how it is goes...

Rajesh...

爱她像谁 2024-12-20 15:05:49

我创建了一个基于ContentProvider的简单SharedPreferences,它可以跨进程使用,你可以从我的bitbucket中找到它们 https://bitbucket.org/ABitNo/zi/src/783b6b451ba1/src/me/abitno/zi/provider/preference

I've created a simple SharedPreferences based on ContentProvider, which can be used accross processes, you can find them from my bitbucket https://bitbucket.org/ABitNo/zi/src/783b6b451ba1/src/me/abitno/zi/provider/preference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文