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

时间:2012-12-13 12:10:26

标签: android

有没有办法在Android中获得未解除的例外情况。 当我的应用程序出错时,我想用邮件发送日志。

谢谢!

2 个答案:

答案 0 :(得分: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();
    }

}

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

ExceptionHandler exp = new ExceptionHandler(getApplicationContext());

答案 1 :(得分:1)

当应用程序正常退出时,您可以记录某些内容,然后您可以检查已保存的数据,以判断它是否已关闭。

相关问题