天天看点

[RK3288][Android6.0] 调试笔记 --- kill掉service后不重启方法

Platform: Rockchip

OS: Android 6.0

Kernel: 3.10.92

应用程序经常会用到service来跑后台服务,然后应用程序被kill掉之后service却还在。

或者你想要kill掉service,但是执行了kill命令之后它有默认启动了。

这是默认的系统行为,下面是解决方法:

如果Service原本就有onStartCommand,那么直接修改返回值,如果没有,那么添加如下code即可:

public int onStartCommand(Intent intent, int flags, int startId) {
    return START_NOT_STICKY;
}
           

参考:

如何提高Service的优先级避免被杀死或者杀死后如何再次重启Service?

Why does my Android service get restarted when the process is killed, even though I used START_NOT_STICKY?

Service总结

Service API changes starting with Android 2.0

Android的服务(Service)(二)Service的自动重启问题