content: package com.pxs.terminal;
  
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.system.Os;
  
  import androidx.annotation.NonNull;
  
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.io.Writer;
  
  public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
      private Context context;
  
      public MyUncaughtExceptionHandler(Context context) {
          this.context = context;
      }
  
      @Override
      public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {
          final Writer result = new StringWriter();
          final PrintWriter printWriter = new PrintWriter(result);
          //如果异常时在AsyncTask里面的后台线程抛出的
          //那么实际的异常仍然可以通过getCause获得
          Throwable cause = throwable;
          while (null != cause) {
              cause.printStackTrace(printWriter);
              cause = cause.getCause();
          }
          //stacktraceAsString就是获取的carsh堆栈信息
          final String stacktraceAsString = result.toString();
          printWriter.close();
  
          Intent intent = new Intent(context, LogActivity.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.putExtra("stack", stacktraceAsString);
          PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3 * 1000, pi);
          android.os.Process.killProcess(android.os.Process.myPid());
      }
  }
  