简体   繁体   English

强制停止后,服务仍然有效

[英]Service still alive after force stop

I'd like to know wether it's normal behaviour for a Service to stay up after user force close app. 我想知道在用户强行关闭应用后,服务保持正常运行是否正常。

My app uses a bound service to handle bluetooth communication in the background, which works pretty well. 我的应用程序使用绑定服务在后台处理蓝牙通信,效果很好。 In the onCreate method of my service I go check a few stuff in my Sqlite DB, but if the app is force closed, the DB is obviously unreachable. 在服务的onCreate方法中,我检查了Sqlite数据库中的一些内容,但是如果强制关闭应用程序,则该数据库显然无法访问。

So when I force stop the app, a few seconds later, the service crashes while trying to read from DB. 因此,当我强制停止应用程序时,几秒钟后,该服务在尝试从数据库读取时崩溃。 Now I could write try/catch to check wether my SQLiteOpenHelper is null, but I'd rather my service to stop on the "force close" action, and avoid to trigger the onCreate method. 现在,我可以编写try / catch来检查我的SQLiteOpenHelper是否为null,但我希望我的服务停止“强制关闭”操作,并避免触发onCreate方法。

The app and service works both pretty well otherwise, but that UI error message bugs me. 否则,该应用程序和服务都可以很好地工作,但是该UI错误消息使我感到困惑。

Process: (Package), PID: 24030 工艺:(包装),PID:24030

java.lang.RuntimeException: Unable to create service (Package).TraitementsAsync.ServiceComm: java.lang.NullPointerException at android.app.ActivityThread.handleCreateService(ActivityThread.java:2746) at android.app.ActivityThread.access$1900(ActivityThread.java:169) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at (Package).service.ServiceBdd.getParametre(ServiceBdd.java:414) at (Package).service.ServiceTipe.getParametre(ServiceTipe.java:699) at (Package).Traite java.lang.RuntimeException:无法创建服务(Package).TraitementsAsync.ServiceComm:android.app.ActivityThread.handleCreateService(ActivityThread.java:2746)处的java.lang.NullPointerException android.app.ActivityThread.access $ 1900(ActivityThread。的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1359)的android.os.Handler.dispatchMessage(Handler.java:102)的android.os.Looper.loop(Looper.java:136)的java:169) )的android.app.ActivityThread.main(ActivityThread.java:5476)处的com.java.lang.reflect.Method.invokeNative(本机方法)处的com。导致dalvik.system.NativeStart.main的android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)创建者:(Package).service.ServiceBdd.getParametre(ServiceBdd.java:414)处(Package).service.ServiceTipe.getParametre(ServiceTipe.java:699)处的java.lang.NullPointerException。 mentsAsync.ServiceComm.onCreate(ServiceComm.java:83) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2736) at android.app.ActivityThread.access$1900(ActivityThread.java:169) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) 在android.app.ActivityThread.handleCreateService(ActivityThread.java:2736)上的mentsAsync.ServiceComm.onCreate(ServiceComm.java:83)在android.app.ActivityThread $上的android.app.ActivityThread.access $ 1900(ActivityThread.java:169) H.handleMessage(ActivityThread.java:1359)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:136)在android.app.ActivityThread.main(ActivityThread .java:5476),位于java.lang.reflect.Method.invokeNative(本地方法),位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run.java.lang.reflect.Method.invoke(Method.java:515) (ZygoteInit.java:1268)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)在dalvik.system.NativeStart.main(本机方法)

According to the question, this is definitely a not normal behavior, in particular, because the service is rising a NullPointerException (NPE). 根据该问题,这绝对是不正常的行为,特别是因为该服务正在NullPointerException (NPE)。 The Android service is used to compute a set of tasks that could be synchronous or not (asynchronous). Android服务用于计算一组可能是同步的或非同步的(异步)任务。 In addition, in both cases, it is a developer responsibility implement a non-blocking and thread-safe mechanisms. 另外,在两种情况下,实施非阻塞和线程安全机制都是开发人员的责任。

As we can see on Android's page, 正如我们在Android页面上看到的那样,

(...) start a service and it will continue to run in the background even if the user switches to another application. (...)启动服务,即使用户切换到另一个应用程序,该服务也将继续在后台运行。 Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC) (...) 此外,组件可以绑定到服务以与其进行交互,甚至可以执行进程间通信(IPC)(...)

In addition, the Android's documentation says that: 另外,Android的文档指出:

A service is "started" when an application component (such as an activity) starts it by calling startService(). 当应用程序组件(例如活动)通过调用startService()启动服务时,将“启动”服务。 Once started, a service can run in the background indefinitely, even if the component that started it is destroyed 启动后,即使启动该服务的组件被破坏,服务也可以无限期在后台运行

Therefor, it is very important, to check the behavior (algorithm) that was implemented on method onStartCommand() , in this case the service can run in the background indefinitely. 因此,检查在onStartCommand()方法上实现的行为(算法)非常重要,在这种情况下,服务可以无限期在后台运行。 If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService() . 如果执行此操作,则有责任通过调用stopSelf()stopService()在服务完成时停止该服务。

Check Android service page to full details. 查看Android服务页面以获取完整详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM