Nikolai
Nikolai

Reputation: 266

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.

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.

The app and service works both pretty well otherwise, but that UI error message bugs me.

Process: (Package), 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).TraitementsAsync.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)

Upvotes: 0

Views: 227

Answers (1)

e2a
e2a

Reputation: 982

According to the question, this is definitely a not normal behavior, in particular, because the service is rising a NullPointerException (NPE). The Android service is used to compute a set of tasks that could be synchronous or not (asynchronous). 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,

(...) 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) (...)

In addition, the Android's documentation says that:

A service is "started" when an application component (such as an activity) starts it by calling 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. If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService().

Check Android service page to full details.

Upvotes: 0

Related Questions