Android - 如何从restartPackage()保存服务?

发布于 2024-11-05 01:16:41 字数 2531 浏览 2 评论 0原文

我制作了一个应用程序,其中包含一项服务,该服务应在我的应用程序启动后立即启动。

但问题是,每当我尝试使用任何任务杀手来终止我的应用程序时,我的服务就会崩溃并显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2024-11-12 01:16:41

当应用程序被终止时,传递给 Intent 的 Context 似乎被取消了。简单的解决方法是:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文