服务的生命周期以及注销监听器的最佳方法

发布于 2024-09-16 17:28:30 字数 1771 浏览 3 评论 0原文

我有一个更新 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 技术交流群。

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

发布评论

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

评论(1

深白境迁sunset 2024-09-23 17:28:30

永远不要在 IntentService 中注册侦听器,就像永远不要在清单注册的 BroadcastReceiver 中注册侦听器一样。这些应该是短暂的对象。您的侦听器将失效的 IntentService 保留在内存中,并在此过程中泄漏内存。

让服务更好吗?
一直运行并清理
onDestroy() 当最后一个小部件被删除时
删除了?

当应用程序让服务一直运行时,用户会非常恼火,除非他们清楚地了解这样做的好处。我会避免这种情况。

Never register a listener in an IntentService, just as you never register a listener in a manifest-registered BroadcastReceiver. These are supposed to be short-lived objects. Your listeners are keeping your defunct IntentService in memory and are leaking memory along the way.

Would it be better to let the Service
run all the time and clean up in
onDestroy() when the last widget is
removed?

User get very irritated when applications keep services running all the time, unless they clearly understand the benefit. I would avoid this.

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