更改侦听器的偏好
我读过这篇有用的文章:
SharedPreferences.onSharedPreferenceChangeListener 没有被一致调用
然而我没有运气。我正在尝试创建一个在服务中运行的 OnSharedPreferenceChangeListener。一切都正确实现,但侦听器并不总是被触发。
public MyServiceOne extends Service {
public SharedPreferences mSharedPreferences;
// Listener defined by anonymous inner class.
public OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d("debug", "A preference has been changed");
}
};
@Override
public void onCreate() {
mSharedPreferences = getSharedPreferences(MySharedPreferences.NAME, Context.MODE_PRIVATE);
mSharedPreferences.registerOnSharedPreferenceChangeListener(mListener);
}
@Override
public void onDestroy() {
super.onDestroy();
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mListener);
}
}
更新
这个问题源于我没有提到的一个事实。我正在运行两个服务,当在 MyService2 中进行共享首选项更改时,不会触发任何内容。在清单中,我定义了在不同进程中运行的服务。有办法让这项工作发挥作用吗?
I have read this helpful post :
SharedPreferences.onSharedPreferenceChangeListener not being called consistently
However I am having no luck. I am trying to create an OnSharedPreferenceChangeListener that runs in a service. Everything is implemented correctly but the listener is not always triggered.
public MyServiceOne extends Service {
public SharedPreferences mSharedPreferences;
// Listener defined by anonymous inner class.
public OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d("debug", "A preference has been changed");
}
};
@Override
public void onCreate() {
mSharedPreferences = getSharedPreferences(MySharedPreferences.NAME, Context.MODE_PRIVATE);
mSharedPreferences.registerOnSharedPreferenceChangeListener(mListener);
}
@Override
public void onDestroy() {
super.onDestroy();
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mListener);
}
}
UPDATE
The problem stems from a fact I did not mention. I am running two services and when shared preference changes are made in MyService2 nothing is triggered. In the manifest I define the services to run in different processes. Is there a way to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在获得一些 Android 经验后,我回来回答这个问题并帮助其他遇到此类情况的人。
OnSharedPreferenceChangeListener 的实现是正确的。问题在于清单和应用程序架构。由于服务在不同的进程中运行,Android 为每个服务创建了不同的 Dalvik 虚拟机,因此它们不会“听到”彼此的侦听器。
这只是糟糕的设计 - 实现运行两个并发服务的更好方法是创建一个服务,将每个“服务”线程化以同时运行。这样他们就可以共享相同的堆,从而共享相同的对象和对象。侦听器
如果有人决定使用两种服务,他们可以创建一个 BroadcastReceiver 来捕获需要更改首选项的意图 - 或通过套接字进行通信。还有一些方法可以通过 ContentProvider 来做到这一点(请永远不要这样做)。但同样,如果设计良好,就没有理由这样做。
After gaining some experience with Android I have come back to answer this question and help anyone else who is having trouble with this type of situation.
The implementation of the OnSharedPreferenceChangeListener is correct. The problem lies in the manifest and application architecture. Because the services are running in different processes Android has created difference Dalvik Virtual Machines for each service therefore they do not "hear" each others listeners.
This was just bad design - The better way to approach the idea of running two concurrent services is to create one service that threads each "service" off to run concurrently. This way they can share the same Heap thus sharing the same Objects & Listeners
If someone were determined to use two services they could create a BroadcastReceiver to catch intents for that preferences need to be changed - or communicate through sockets. There are also ways to do this with a ContentProvider (Please NEVER DO THIS). But again, there is no reason for any of this if the design is good.
为什么不让您的服务实现
OnSharedPreferenceChangeListener
?这样它就不是匿名内部类,因此它不会被垃圾收集,并且您仍然可以在服务中执行任何您想要的操作。
Why not let your service implement
OnSharedPreferenceChangeListener
?That way it's not an anonymous inner class, so it doesn't get garbage collected, and you can still do whatever you want in the service.
我知道这个线程很旧。但是有人尝试过使用 MODE_MULTI_PROCESS 吗?
MODE_MULTI_PROCESS
I know this thread is old. But has anyone tried this using MODE_MULTI_PROCESS?
MODE_MULTI_PROCESS