在开发过程中,虽然经过测试,但在发布后,在广大用户各种各样的运行环境和操作下,可能会发生一些异想不到的错误导致程序崩溃。将这些错误信息收集起来并反馈给开发者,对于开发者改进优化程序是相当重要的。好了,下面就来实现这种功能吧。
(更正:2012年2月9日18时42分07秒)
由于为历史帖原因,以下做法比较浪费,但抓取异常的效果是一样的。
1.对于UI线程(即Android中的主线程)抛出的未捕获异常,将这些异常信息存储起来然后关闭到整个应用程序。并再次启动程序,则进入崩溃信息反馈界面让用户将出错信息以Email的形式发送给开发者。
2.对于非UI线程抛出的异常,则立即唤醒崩溃信息反馈界面提示用户将出错信息发送Email。
效果图如下[CSDN]:
过程了解了,则需要了解的几个知识点如下:
1.拦截UncaughtException
Application.onCreate()是整个Android应用的入口方法。在该方法中执行如下代码即可拦截UncaughtException:
- ueHandler = new UEHandler(this);
-
- Thread.setDefaultUncaughtExceptionHandler(ueHandler);
2.抓取导致程序崩溃的异常信息
UEHandler是Thread.UncaughtExceptionHandler的实现类,在其public void uncaughtException(Thread thread, Throwable ex)的实现中可以获取崩溃信息,代码如下:
-
- String info = null;
- ByteArrayOutputStream baos = null;
- PrintStream printStream = null;
- try {
- baos = new ByteArrayOutputStream();
- printStream = new PrintStream(baos);
- ex.printStackTrace(printStream);
- byte[] data = baos.toByteArray();
- info = new String(data);
- data = null;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (printStream != null) {
- printStream.close();
- }
- if (baos != null) {
- baos.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
3.程序抛异常后,要关闭整个应用
悲催的程序员,唉,以下三种方式都无效了,咋办啊!!!
3.1android.os.Process.killProcess(android.os.Process.myPid());
3.2ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.restartPackage("lab.sodino.errorreport");
3.3System.exit(0)
好吧,毛主席告诉我们:自己动手丰衣足食。
SoftApplication中声明一个变量need2Exit,其值为true标识当前的程序需要完整退出;为false时该干嘛干嘛去。该变量在应用的启动Activity.onCreate()处赋值为false。
在捕获了崩溃信息后,调用SoftApplication.setNeed2Exit(true)标识程序需要退出,并finish()掉ActErrorReport,这时ActErrorReport退栈,抛错的ActOccurError占据手机屏幕,根据Activity的生命周期其要调用onStart(),则我们在onStart()处读取need2Exit的状态,若为true,则也关闭到当前的Activity,则退出了整个应用了。此方法可以解决一次性退出已开启了多个Activity的Application。详细代码请阅读下面的示例源码。
好了,代码如下:
lab.sodino.errorreport.SoftApplication.java
- package lab.sodino.errorreport;
- import java.io.File;
- import android.app.Application;
-
-
-
-
- public class SoftApplication extends Application {
-
- public static final String PATH_ERROR_LOG = File.separator + "data" + File.separator + "data"
- + File.separator + "lab.sodino.errorreport" + File.separator + "files" + File.separator
- + "error.log";
-
- private boolean need2Exit;
-
- private UEHandler ueHandler;
- public void onCreate() {
- need2Exit = false;
- ueHandler = new UEHandler(this);
-
- Thread.setDefaultUncaughtExceptionHandler(ueHandler);
- }
- public void setNeed2Exit(boolean bool) {
- need2Exit = bool;
- }
- public boolean need2Exit() {
- return need2Exit;
- }
- }
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;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- softApplication = (SoftApplication) getApplication();
-
- 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();
- }
- });
-
- 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 {
-
- }
- }
-
-
-
-
-
-
- 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;
- }
- }
lab.sodino.errorreport.ActErrorReport.java
lab.sodino.errorreport.UEHandler.java
- package lab.sodino.uncaughtexception;
-
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.PrintStream;
- import android.content.Intent;
- import android.util.Log;
-
-
-
-
-
- public class UEHandler implements Thread.UncaughtExceptionHandler {
- private SoftApplication softApp;
- private File fileErrorLog;
-
- public UEHandler(SoftApplication app) {
- softApp = app;
- fileErrorLog = new File(SoftApplication.PATH_ERROR_LOG);
- }
-
- @Override
- public void uncaughtException(Thread thread, Throwable ex) {
-
- String info = null;
- ByteArrayOutputStream baos = null;
- PrintStream printStream = null;
- try {
- baos = new ByteArrayOutputStream();
- printStream = new PrintStream(baos);
- ex.printStackTrace(printStream);
- byte[] data = baos.toByteArray();
- info = new String(data);
- data = null;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (printStream != null) {
- printStream.close();
- }
- if (baos != null) {
- baos.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- long threadId = thread.getId();
- Log.d("ANDROID_LAB", "Thread.getName()=" + thread.getName() + " id=" + threadId + " state=" + thread.getState());
- Log.d("ANDROID_LAB", "Error[" + info + "]");
- if (threadId != 1) {
-
- Intent intent = new Intent(softApp, ActErrorReport.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.putExtra("error", info);
- intent.putExtra("by", "uehandler");
- softApp.startActivity(intent);
- } else {
-
- Intent intent = new Intent(softApp, ActOccurError.class);
-
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- softApp.startActivity(intent);
-
- write2ErrorLog(fileErrorLog, info);
-
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- }
-
- private void write2ErrorLog(File file, String content) {
- FileOutputStream fos = null;
- try {
- if (file.exists()) {
-
- file.delete();
- } else {
- file.getParentFile().mkdirs();
- }
- file.createNewFile();
- fos = new FileOutputStream(file);
- fos.write(content.getBytes());
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
/res/layout/main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Throws Exception By Main Thread"
- android:id="@+id/btnThrowMain"
- ></Button>
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Throws Exception By Child Thread"
- android:id="@+id/btnThrowChild"
- ></Button>
- </LinearLayout>
/res/layout/report.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/errorHint"
- android:id="@+id/txtErrorHint" />
- <EditText android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:id="@+id/editErrorContent"
- android:editable="false" android:layout_weight="1"></EditText>
- <LinearLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:background="#96cdcd"
- android:gravity="center" android:orientation="horizontal">
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="Report"
- android:id="@+id/btnREPORT" android:layout_weight="1"></Button>
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="Cancel"
- android:id="@+id/btnCANCEL" android:layout_weight="1"></Button>
- </LinearLayout>
- </LinearLayout>
用到的string.xml资源为:
- <string name="errorHint">A error has happened %1$s.Please click <i><b>"REPORT"</b></i> to send the error information to us by email, Thanks!!!</string>
重要的一点是要在AndroidManifest.xml中对<application>节点设置android:name=".SoftApplication"
本文内容归CSDN博客博主Sodino 所有
转载请注明出处: http://blog.csdn.net/sodino/archive/2011/06/13/6540329.aspx