应用程序在 Android 被杀死时执行的方法
Executing method when app is killed on Android
我构建了一个 Android 应用程序。如果我定期通过后退按钮终止我的应用程序,则会执行 onDestroy()
。但是当我终止我的应用程序时(使用最近的应用程序按钮),onDestroy()
永远不会执行。我现在在我的 Activity
的 onCreate(Bundle savedInstanceState)
方法中添加了以下内容:
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
稍后在正文中Activity
class我补充说:
private boolean isUIThread(){
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
public void handleUncaughtException(Thread thread, Throwable e) {
if(isUIThread()) {
// exception occurred from UI thread
} else { //handle non UI thread throw uncaught exception
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//handle non UI thread throw uncaught exception
}
});
}
}
不幸的是,当我终止我的应用程序时,handleUncaughtException()
方法从未被调用(当我通过后退按钮定期终止我的应用程序时,它也不会被调用)。
怎么了?
我正在使用 Android 5.1.
Unfortunately the handleUncaughtException()
method gets never called when I kill my application (it also gets not called when I regularly terminate my application through the back button).
Catching an exception(在 dalvik 内)和终止应用程序 (Linux) 过程是不同的se。在这种情况下,不会调用 handleUncaughtException()
方法,除非应用抛出未捕获的异常。
Executing method when app is killed on Android
从最近使用的应用程序中删除应用程序时,只有 Activity
的 onPause()
保证会被调用。但你可能会被 Service
的回调 "informed"。可以找到一些额外的信息 or 。
我构建了一个 Android 应用程序。如果我定期通过后退按钮终止我的应用程序,则会执行 onDestroy()
。但是当我终止我的应用程序时(使用最近的应用程序按钮),onDestroy()
永远不会执行。我现在在我的 Activity
的 onCreate(Bundle savedInstanceState)
方法中添加了以下内容:
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
稍后在正文中Activity
class我补充说:
private boolean isUIThread(){
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
public void handleUncaughtException(Thread thread, Throwable e) {
if(isUIThread()) {
// exception occurred from UI thread
} else { //handle non UI thread throw uncaught exception
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//handle non UI thread throw uncaught exception
}
});
}
}
不幸的是,当我终止我的应用程序时,handleUncaughtException()
方法从未被调用(当我通过后退按钮定期终止我的应用程序时,它也不会被调用)。
怎么了?
我正在使用 Android 5.1.
Unfortunately the
handleUncaughtException()
method gets never called when I kill my application (it also gets not called when I regularly terminate my application through the back button).
Catching an exception(在 dalvik 内)和终止应用程序 (Linux) 过程是不同的se。在这种情况下,不会调用 handleUncaughtException()
方法,除非应用抛出未捕获的异常。
Executing method when app is killed on Android
从最近使用的应用程序中删除应用程序时,只有 Activity
的 onPause()
保证会被调用。但你可能会被 Service
的回调 "informed"。可以找到一些额外的信息