在Android中如何检查应用程序上次强制关闭

Ati*_*ood 1 android

有没有办法在Android中获得未开辟的例外.当我的应用程序出错时,我想使用邮件发送日志.

谢谢!

Ali*_*ran 5

这里有一些可以帮助你的代码.

首先创建一个类

import java.lang.Thread.UncaughtExceptionHandler;
import android.content.Context;
import android.content.SharedPreferences;

public class ExceptionHandler implements UncaughtExceptionHandler {

    private Context context;

    public ExceptionHandler(Context context) {
        super();
        this.context = context;

    }

    public void uncaughtException(Thread thread, Throwable ex) {
        SharedPreferences prefs = context.getSharedPreferences("Your_preference_name", Context.MODE_PRIVATE);
        prefs.edit().putBoolean("is_force_closed", true).commit();
    }

}
Run Code Online (Sandbox Code Playgroud)

然后在应用程序的主要活动中初始化此类

ExceptionHandler exp = new ExceptionHandler(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)

  • @solilo但app只强制关闭未捕获的异常:) (2认同)