服务的生命周期以及注销监听器的最佳方法
我有一个更新 AppWidget 的 IntentService 。每次将 AppWidget 放置在屏幕上或调用 AlarmManager /ContentObserver / OnChangeListener 时,该服务都会启动。更新小部件后它会自行停止。
UpdateService.java:
@Override
public void onCreate() {
contentObserver = new CustomContentObserver();
registerContentObserver(contentObserver);
/* Am I registering several instances here or is this fine? */
onChangeListener = new CustomSharedPreferencesOnChangeListener();
sharedPreferences.registerOnSharedPreferenceChangeListener(onChangeListener);
Intent updateIntent = new Intent(this, UpdateService.class);
pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
}
@Override
public void onStart(Intent intent, int startId) {
if(AppWidget.DISABLE.equals(intent.getAction())) {
unregisterContentObserver(contentObserver);
sharedPreferences.unregisterOnSharedPreferenceChangeListener(onChangeListener);
alarmManager.cancel(pendingIntent);
} else {
/* Scheduling next update */
alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdateTime, pendingIntent);
/* Updating the Widget ... */
updateWidget();
/* Stopping the Service */
stopSelf();
}
}
当没有要更新的小部件时,该服务不应启动,因此我必须取消注册侦听器和观察者并取消挂起的意图。
我想知道清理资源的最佳方法是什么?启动服务来停止对我来说似乎有点尴尬。
AppWidget.java:
@Override
public void onDisabled(Context context) {
Intent intent = new Intent(context, UpdateService.class);
intent.setAction(DISABLE);
context.startService(intent);
context.stopService(intent);
super.onDisabled(context);
}
我还想知道启动和停止服务的成本。让 Service 一直运行并在最后一个小部件被删除时在 onDestroy() 中进行清理会更好吗?
I've an IntentService which updates an AppWidget. That Service starts every time the AppWidget is being placed on the Screen or when the AlarmManager /ContentObserver / OnChangeListener get called. It stops itself after updating the widget.
UpdateService.java:
@Override
public void onCreate() {
contentObserver = new CustomContentObserver();
registerContentObserver(contentObserver);
/* Am I registering several instances here or is this fine? */
onChangeListener = new CustomSharedPreferencesOnChangeListener();
sharedPreferences.registerOnSharedPreferenceChangeListener(onChangeListener);
Intent updateIntent = new Intent(this, UpdateService.class);
pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
}
@Override
public void onStart(Intent intent, int startId) {
if(AppWidget.DISABLE.equals(intent.getAction())) {
unregisterContentObserver(contentObserver);
sharedPreferences.unregisterOnSharedPreferenceChangeListener(onChangeListener);
alarmManager.cancel(pendingIntent);
} else {
/* Scheduling next update */
alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdateTime, pendingIntent);
/* Updating the Widget ... */
updateWidget();
/* Stopping the Service */
stopSelf();
}
}
The service shouldn't start when there is no widget to update, so I have to unregister the listener and observer and cancel the pending intent.
I am wondering what the best way is to clean up the resources? Starting the service to stop it seems a bit awkward to me.
AppWidget.java:
@Override
public void onDisabled(Context context) {
Intent intent = new Intent(context, UpdateService.class);
intent.setAction(DISABLE);
context.startService(intent);
context.stopService(intent);
super.onDisabled(context);
}
I am also wondering about the costs of starting and stopping a service. Would it be better to let the Service run all the time and clean up in onDestroy() when the last widget is removed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
永远不要在
IntentService
中注册侦听器,就像永远不要在清单注册的BroadcastReceiver
中注册侦听器一样。这些应该是短暂的对象。您的侦听器将失效的IntentService
保留在内存中,并在此过程中泄漏内存。当应用程序让服务一直运行时,用户会非常恼火,除非他们清楚地了解这样做的好处。我会避免这种情况。
Never register a listener in an
IntentService
, just as you never register a listener in a manifest-registeredBroadcastReceiver
. These are supposed to be short-lived objects. Your listeners are keeping your defunctIntentService
in memory and are leaking memory along the way.User get very irritated when applications keep services running all the time, unless they clearly understand the benefit. I would avoid this.