Android小技巧(一):实现捕获应用的运行时异常

转载请注明出处!本博客地址:http://blog.csdn.net/mylzc

由于Android设备各异,第三方定制的Android系统也非常多,我们不可能对所有的设备场景都进行测试,因而开发一款完全无bug的应用几乎是不可能的任务,那么当应用在用户的设备上Force Close时,我们是不是可以捕获这个错误,记录用户的设备信息,然后让用户选择是否反馈这些堆栈信息,通过这种bug反馈方式,我们可以有针对性地对bug进行修复。

当我们的的应用由于运行时异常导致Force Close的时候,可以设置主线程的UncaughtExceptionHandler,实现捕获运行时异常的堆栈信息。同时用户可以把堆栈信息通过发送邮件的方式反馈给我们。下面是实现的代码:

代码下载请按此

例子:点击按钮后,会触发一个NullPointerException的运行时异常,这个例子实现了捕获运行时异常,发送邮件反馈设备和堆栈信息的功能。

界面1(触发运行时异常)

Android小技巧(一):实现捕获应用的运行时异常

界面2(发送堆栈信息)

Android小技巧(一):实现捕获应用的运行时异常

TestActivity.java

  1. package com.zhuozhuo; 
  2.  
  3. import java.io.PrintWriter; 
  4. import java.io.StringWriter; 
  5. import java.lang.Thread.UncaughtExceptionHandler; 
  6.  
  7. import android.app.Activity; 
  8. import android.content.Intent; 
  9. import android.net.Uri; 
  10. import android.os.Build; 
  11. import android.os.Bundle; 
  12. import android.util.Log; 
  13. import android.view.View; 
  14. import android.view.View.OnClickListener; 
  15. import android.widget.EditText; 
  16. import android.widget.TextView; 
  17.  
  18. publicclass TestActivity extends Activity { 
  19.     /** Called when the activity is first created. */ 
  20.  
  21.  
  22.     @Override 
  23.     publicvoid onCreate(Bundle savedInstanceState) { 
  24.         super.onCreate(savedInstanceState); 
  25.         setContentView(R.layout.main); 
  26.         Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {//给主线程设置一个处理运行时异常的handler  
  27.  
  28.             @Override 
  29.             publicvoid uncaughtException(Thread thread, final Throwable ex) { 
  30.  
  31.                 StringWriter sw = new StringWriter(); 
  32.                 PrintWriter pw = new PrintWriter(sw); 
  33.                 ex.printStackTrace(pw); 
  34.                  
  35.                 StringBuilder sb = new StringBuilder(); 
  36.                  
  37.                 sb.append("Version code is "); 
  38.                 sb.append(Build.VERSION.SDK_INT + " ");//设备的Android版本号  
  39.                 sb.append("Model is "); 
  40.                 sb.append(Build.MODEL+" ");//设备型号  
  41.                 sb.append(sw.toString()); 
  42.  
  43.                 Intent sendIntent = new Intent(Intent.ACTION_SENDTO); 
  44.                 sendIntent.setData(Uri.parse("mailto:csdn@csdn.com"));//发送邮件异常到csdn@csdn.com邮箱  
  45.                 sendIntent.putExtra(Intent.EXTRA_SUBJECT, "bug report");//邮件主题  
  46.                 sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());//堆栈信息  
  47.                 startActivity(sendIntent); 
  48.                 finish(); 
  49.             } 
  50.         }); 
  51.          
  52.         findViewById(R.id.button).setOnClickListener(new OnClickListener() { 
  53.              
  54.             @Override 
  55.             publicvoid onClick(View v) { 
  56.                 Integer a = null
  57.                 a.toString();//触发nullpointer运行时错误  
  58.                  
  59.             } 
  60.         }); 
  61.          
  62.     } 
package com.zhuozhuo;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity extends Activity {
	/** Called when the activity is first created. */


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {//给主线程设置一个处理运行时异常的handler

			@Override
			public void uncaughtException(Thread thread, final Throwable ex) {

				StringWriter sw = new StringWriter();
				PrintWriter pw = new PrintWriter(sw);
				ex.printStackTrace(pw);
				
				StringBuilder sb = new StringBuilder();
				
				sb.append("Version code is ");
				sb.append(Build.VERSION.SDK_INT + "
");//设备的Android版本号
				sb.append("Model is ");
				sb.append(Build.MODEL+"
");//设备型号
				sb.append(sw.toString());

				Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
				sendIntent.setData(Uri.parse("mailto:csdn@csdn.com"));//发送邮件异常到csdn@csdn.com邮箱
				sendIntent.putExtra(Intent.EXTRA_SUBJECT, "bug report");//邮件主题
				sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());//堆栈信息
				startActivity(sendIntent);
				finish();
			}
		});
		
		findViewById(R.id.button).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Integer a = null;
				a.toString();//触发nullpointer运行时错误
				
			}
		});
		
	}
}

main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.     <TextViewandroid:layout_width="fill_parent" 
  6.         android:layout_height="wrap_content"android:text="@string/hello"/> 
  7.     <EditTextandroid:id="@+id/editText1"android:layout_width="match_parent" 
  8.         android:text="点击按钮触发运行时异常"android:layout_height="wrap_content" 
  9.         android:layout_weight="1"android:gravity="top"></EditText> 
  10.     <Buttonandroid:text="按钮"android:id="@+id/button" 
  11.         android:layout_width="wrap_content"android:layout_height="wrap_content" 
  12.         android:layout_gravity="center_horizontal"></Button> 
  13. </LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
	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" />
	<EditText android:
		android:text="点击按钮触发运行时异常" android:layout_height="wrap_content"
		android:layout_weight="1" android:gravity="top"></EditText>
	<Button android:text="按钮" android:
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_gravity="center_horizontal"></Button>
</LinearLayout>

转载请注明出处!本博客地址:http://blog.csdn.net/mylzc