Android实现将应用崩溃信息发送给开发者并重启应用的方法

2019-12-10 18:53:16刘景俊

lab.sodino.errorreport.ActOccurError.java

package lab.sodino.errorreport;
import java.io.File;
import java.io.FileInputStream;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class ActOccurError extends Activity {
  private SoftApplication softApplication;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    softApplication = (SoftApplication) getApplication();
    // 一开始进入程序恢复为"need2Exit=false"。
    softApplication.setNeed2Exit(false);
    Log.d("ANDROID_LAB", "ActOccurError.onCreate()");
    Button btnMain = (Button) findViewById(R.id.btnThrowMain);
    btnMain.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        Log.d("ANDROID_LAB", "Thread.main.run()");
        int i = 0;
        i = 100 / i;
      }
    });
    Button btnChild = (Button) findViewById(R.id.btnThrowChild);
    btnChild.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        new Thread() {
          public void run() {
            Log.d("ANDROID_LAB", "Thread.child.run()");
            int i = 0;
            i = 100 / i;
          }
        }.start();
      }
    });
    // 处理记录于error.log中的异常
    String errorContent = getErrorLog();
    if (errorContent != null) {
      Intent intent = new Intent(this, ActErrorReport.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.putExtra("error", errorContent);
      intent.putExtra("by", "error.log");
      startActivity(intent);
    }
  }
  public void onStart() {
    super.onStart();
    if (softApplication.need2Exit()) {
      Log.d("ANDROID_LAB", "ActOccurError.finish()");
      ActOccurError.this.finish();
    } else {
      // do normal things
    }
  }
  /**
   * 读取是否有未处理的报错信息。<br/>
   * 每次读取后都会将error.log清空。<br/>
   *
   * @return 返回未处理的报错信息或null。
   */
  private String getErrorLog() {
    File fileErrorLog = new File(SoftApplication.PATH_ERROR_LOG);
    String content = null;
    FileInputStream fis = null;
    try {
      if (fileErrorLog.exists()) {
        byte[] data = new byte[(int) fileErrorLog.length()];
        fis = new FileInputStream(fileErrorLog);
        fis.read(data);
        content = new String(data);
        data = null;
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (fis != null) {
          fis.close();
        }
        if (fileErrorLog.exists()) {
          fileErrorLog.delete();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return content;
  }
}