gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:42:49 25 4
gpt4 key购买 nike

我制作了一个应用程序,该应用程序应在我的应用程序启动后立即启动。

但是问题是无论何时我尝试使用那里的任何Task Killer杀死我的应用程序,然后我的服务就会崩溃并显示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)

最佳答案

当应用程序被杀死时,传递给Intent的Context似乎无效。简单的解决方法是:

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

关于android - Android-如何从restartPackage()保存服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5884292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com