从Thread.UncaughtExceptionHandler启动服务? [英] Start a service from Thread.UncaughtExceptionHandler?
问题描述
我正在尝试设置全局异常处理服务,如这个答案。这种方法听起来很合乎逻辑,因为崩溃后,我的自定义 Thread.UncaughtExceptionHandler
可能无法成功执行,因为应用程序可能未处于稳定状态,或者因为在进程被操作系统杀死之前可能没有足够的时间。
I'm trying to setup a global exception handling service as mentioned in this answer. This approach sounds logical because after a crash, the code in my custom Thread.UncaughtExceptionHandler
may not execute successfully, either because the application may not be in a stable state or because there may not be enough time before the process is killed by the OS.
不幸的是,这种方法似乎不起作用,因为服务在同一进程中启动,所以也被杀死。如何从处理程序的 uncaughtException()
启动服务?我可以强制服务在一个完全独立的过程中启动吗?
Unfortunately, this approach doesn't seem to work because the service launches in the same process so it, too, is killed. How can I start a service from the handler's uncaughtException()
? Can I force the service to launch in a completely seperate process?
我设置了我的自定义默认值(VM级别)应用程序中的异常处理程序
像这样:
I'm setting my custom default (VM-level) exception handler in Application
like this:
public class MyApp extends Application {
@Override
public void onCreate() {
Thread.setDefaultUncaughtExceptionHandler(
MyExceptionReporter.getInstance(this));
...
在我的自定义处理程序中,我启动了这样的服务:
In my custom handler, I'm launching the service like this:
final Intent i = new Intent(MyExceptionService.INTENT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(MyExceptionService.EXTRA_STACK_TRACE,
Log.getStackTraceString(ex));
mContext.startService(i);
该服务在 AndroidMaifest.xml
文件,并从应用程序的其他地方启动。
The service is declated in the AndroidMaifest.xml
file and launches fine from elsewhere in the application.
推荐答案
啊,我想出了这一点。关键是将 android:process
添加到清单中,这将强制服务在单独的进程中启动。这样可以防止服务在应用程序被杀时被杀死。
Ah, I figured this out. The key was to add android:process
to the manifest, which forces the service to launch in a seperate process. This prevents the service from being killed when the application is killed.
<service
android:name="your.package.blah.blah.MyExceptionService"
android:process=":exception" >
<intent-filter>
<action android:name="your.package.blah.blah.REPORT" />
</intent-filter>
</service>
这篇关于从Thread.UncaughtExceptionHandler启动服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!