Android - 如何从restartPackage()保存服务?
我制作了一个应用程序,其中包含一项服务,该服务应在我的应用程序启动后立即启动。
但问题是,每当我尝试使用任何任务杀手来终止我的应用程序时,我的服务就会崩溃并显示 NullPointerException。据我所知,这些任务杀手使用包名称调用 restartPackage() 。
这就是我开始服务的方式:-
Intent intent = new Intent(myContext, BackService.class);
intent.putExtra("input", duration);
startService(intent);
在杀死我的应用程序后,我收到 NullPointerException:-
Long duration = intent.getLongExtra("input", 199999999);
知道如何解决这个问题吗?
堆栈跟踪:-
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)
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)
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当应用程序被终止时,传递给 Intent 的 Context 似乎被取消了。简单的解决方法是:
It looks like the Context being passed to the Intent is being nullified when the app is killed. The easy fix is: