申请结束后粘滞服务重新启动 [英] Sticky Service restarts after Application close

查看:102
本文介绍了申请结束后粘滞服务重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的粘滞服务的烦恼。我把它在我的MainActivity.class并将其绑定:

i have troubles with my STICKY Service. I call it in my MainActivity.class and bind it:

MainActivity

Intent intent = new Intent(this, MyService.class);
ComponentName MyCompName= startService(intent);
bindService(intent, MyConnection, Context.BIND_AUTO_CREATE);

和...

为MyService

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    return START_STICKY;
}

我想这项服务运行粘稠,永远不会被关闭或重新启动。但是,当我闭上我的应用程序时,的onCreate() - 为MyService的方法被调用,所有变量将重设,并且我不知道为什么。

I want that this Service runs STICKY and never be closed or restarted. But when i close my Application, the onCreate()-Method of MyService is called and all variables are reseted, and i don´t know why.

BTW:我不叫stopService

BTW: I don´t call stopService!

推荐答案

粘并NON_STICKY服务之间的差别在于粘滞服务被杀死之后重新启动。我不认为这是可能的,以保证您的服务将永远不会被重新启动 - 如果内存不足,可能需要重新启动

The difference between STICKY and NON_STICKY services is that STICKY services are restarted after being killed. I don't think it's possible to guarantee that your service will never be restarted - if memory is low it might be restarted.

如果您需要preserve的状态,你可以在数据库中保存的变量。要查看是否是第一次创建或重新启动该服务,您可以检查是否在onStartCommand意图为null。

If you need to preserve the state, you can save variables in a database. To see if the service is being created for the first time or restarted, you can check if the intent in onStartCommand is null.

如果你只在创建服务时需要preserve的初始状态,您可以使用START_REDELIVER_INTENT将重新发送用于创建onStartCommand服务的意图。

If you only need to preserve the initial state when the service was created, you can use START_REDELIVER_INTENT which will resend the Intent used to create the service in onStartCommand.

这篇关于申请结束后粘滞服务重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆