防止Android“过程不良";错误 [英] Prevent Android "process is bad" error

查看:63
本文介绍了防止Android“过程不良";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该过程很糟糕错误似乎没有得到很好的记录,我只能找到解决方法.相反,我对这个错误的原因以及如何防止它发生感兴趣,而不是如何通过重新启动,重新启动应用程序等手动处理它.

The process is bad error does not seem to be very well documented, and I have only been able to find workarounds. I am instead interested in the cause of this error and how to prevent it from happening, and not how to manually handle it through rebooting, relaunching app, etc.

我的特定应用程序使用AlarmManager启动IntentService,该服务每30秒运行一次〜10s.这是在创建应用程序时调用的:

My particular application uses an AlarmManager to launch an IntentService which runs for ~10s every ~30s. This is called on creation of the application:

Intent serviceIntent = new Intent(appContext, MyService.class);
    serviceIntent.putExtra("service_extra", extra);
    launchService = PendingIntent.getService(
            appContext,
            LAUNCH_SERVICE_REQUEST_CODE,
            scanIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );
    alarmManager.setInexactRepeating(
            AlarmManager.RTC,
            System.currentTimeMillis() + interval,
            interval,
            launchService
    );

大多数情况下,这可以按预期进行.但是,有时服务会无限期地无法启动,有时一次无法启动.每隔30秒就会看到以下错误,因此我知道我的警报正在尝试按计划启动IntentService,但是尝试失败并出现进程是错误的错误.

This works as expected most of the time. However, very occasionally the service will fail to launch for an indefinite period, sometimes hours at a time. The following error is seen every ~30s, so I know my alarms are attempting to launch the IntentService as scheduled, but the attempt fails with a process is bad error.

Unable to launch app com.example.android/10024 for service Intent { cmp=com.example.android/.MyService (has extras) }: process is bad

只需重新打开应用即可解决此问题.但是我需要知道如何预防!该服务的目的是在活动被停止或破坏时在后台运行,因此它必须在没有任何用户交互或解决方法的情况下防止此错误.

This is fixed by simply reopening the app. But I need to know how to prevent it! The purpose of this service is to run in the background while activities are stopped or destroyed, so it must prevent this error without any user interaction or workaround.

推荐答案

好机会是它与活动生命周期的处理有关.

Good Chance is that it has to do with the handling of the activity's lifecycle.

这让我想起了我最近在没有完全理解的情况下解决的一个案例,因为该文档不是非常具体地介绍了在Activity上调用onStop()后可以依赖的内容.而你到底不应该做的...

It reminds me of a case I recently solved without fully understanding it because the documentation is not very specific on what you can rely on after onStop() is called on your Activity. And what exactly you shouldn't do...

所以我只能给你一些提示:

So I can only give you some hints:

如果我的猜测正确,那么当您的应用程序(部分)从内存中删除时,您可能无法正确还原. 尝试在开发人员设置中将后台应用程序的限制设置为无". 我怀疑通过此设置,将其放到背景中可以完全再现您的问题.否则,您可能会在这里停止阅读.

If my guess is right, you have problem with not being properly restored, when your App is (partially) removed from memory. Try setting the limit for background-Apps in the developer-settings to "none". I suspect that with this setting your problem will be completly reproducable when puting it to background. Otherwise you probably can stop reading here.

在onStop()之后,我有一个继续运行的线程,该线程也保留了一些引用.停止onPause()中的线程并等待它结束就解决了我的问题.我还需要同步onStop(),因为阻止onPause()会导致onStop()进入并行状态.我还需要将一些初始化工作从onCreate()移到onResume().但是,一旦我弄清楚了要在onResume()和onStop()中删除的内容,那就很明显了.

I had a thread that kept on running after onStop() that also kept some references. Stopping the thread in onPause() and also waiting for it to finsh solved my problem. I also needed to sync my onStop() because blocking onPause() resulted in onStop() coming in paralell. I also needed to move some of my initilization from onCreate() to onResume(). But that was quite obvious once I figured out at what to tear down in onResume() and onStop().

带静电的尖端可能朝相同的方向移动.

Probably the tipp with the static goes in the same direction.

这篇关于防止Android“过程不良";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆