开发者

Android - How to save a Service from restartPackage()?

I have made an app which has a service which should be started as soon as my app starts.

But the problem is whenever i try to kill my app using any Task Killer out there then my service is getting crashed and showing me NullPointerException. And as far as i know these task killer call restartPackage() using package name.

This is how i am starting my service :-

Intent intent = new Intent(myContext, BackService.class);
intent.putExtra("input", duration);
startService(intent);

and here i am getting the NullPointerException after killing my app:-

Long duration = intent.getLongExtra("input", 199999999);

Any idea how to solve this issue?

Stack Trace :-

java.lang.RuntimeException: Unable to start service com.varundroid.util.manager.BackService@47b8ee08 with null: java.lang.NullPointerException
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
    at android.app.ActivityThread.access$3600(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.varundroid.util.manager.BackService.onStart(BackService.java:77)
    at android.app.Service.onStartCommand(Service.java:420)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
    ... 10 more
java.lang.NullPointerException
    at com.varundroid.util.manager.BackService.onStart(BackService.java:77)
    at android.app.Service.onStartCommand(Service.java:420)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
    at android.app.ActivityThread.access$3600(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
 开发者_如何学JAVA   at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
    at dalvik.system.NativeStart.main(Native Method)


It looks like the Context being passed to the Intent is being nullified when the app is killed. The easy fix is:

Long duration = 199999999;
if(intent!=null){
    duration = intent.getLongExtra("input", 199999999);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜