【问题标题】:start activity in custom UncaughtExceptionHandler class not working any more在自定义 UncaughtExceptionHandler 类中启动活动不再起作用
【发布时间】:2019-03-17 07:54:50
【问题描述】:

我曾经用这个类处理异常错误并将错误作为字符串传递给另一个活动以进行显示,但是这种方式不再起作用了,似乎当杀死进程发生“sag.class”活动不能再启动了

public class ExceptionHandler extends MainActivity implements java.lang.Thread.UncaughtExceptionHandler {

    private final Context myContext;

    private SharedPreferences prefs;

    private StringBuilder errorReport;

    public ExceptionHandler(Context context) {
        myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {



        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        System.err.println(stackTrace);

        exception.printStackTrace(new PrintWriter(stackTrace));
        errorReport = new StringBuilder();
        errorReport.append("************ CAUSE OF ERROR ************\n\n");
        errorReport.append(stackTrace.toString());

        errorReport.append("\n************ DEVICE INFORMATION ***********\n");

        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Product: ");
        errorReport.append(Build.PRODUCT);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("\n************ FIRMWARE ************\n");
        errorReport.append("SDK: ");
        errorReport.append(Build.VERSION.SDK);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Release: ");
        errorReport.append(Build.VERSION.RELEASE);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Incremental: ");
        errorReport.append(Build.VERSION.INCREMENTAL);
        errorReport.append(LINE_SEPARATOR);


        errorReport.append("App Version: ");
        try {
            errorReport.append(myContext.getApplicationContext().getPackageManager().getPackageInfo(myContext.getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        errorReport.append(LINE_SEPARATOR);


        prefs = myContext.getSharedPreferences("MyPrefsFile", MODE_PRIVATE);

        if (prefs.contains("access_token"))
        {
            errorReport.append(prefs.getString("fname","")+" "+prefs.getString("lname",""));
        }


        Intent intent = new Intent(myContext, sag.class);
        intent.putExtra("error", errorReport.toString());
        myContext.startActivity(intent);

        Process.killProcess(Process.myPid());
        System.exit(10);

    }


}

那么如何以新的方式在 UncaughtExceptionHandler 类中启动活动呢?

【问题讨论】:

    标签: android android-thread


    【解决方案1】:

    试试这个,请添加此代码以开始新活动

    Intent startAppIntent = new Intent(this, sag.class);
            startAppIntent.putExtra("error", errorReport.toString());
            startAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startAppIntent);
            System.exit(1);
    

    如果this 不起作用,您可以将this 更改为myContext。请让我们知道此代码是否适合您。

    【讨论】:

    • @erfan 我得到了同样奇怪的行为。 Intent 没有触发。你找到解决这个问题的方法了吗?如果是的话,你能分享一下吗?提前致谢。
    【解决方案2】:

    简答:是的,您可以从UncaughtExceptionHandler 开始活动。

    fun restartApp(context : Context) {
        val intent = Intent(context, Destination::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
        android.os.Process.killProcess(android.os.Process.myPid())
        exitProcess(0)
    }
    

    你也可以在这个link找到退出状态号

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 2023-03-18
      • 2011-03-20
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      相关资源
      最近更新 更多