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

来源:互联网 发布:电子兑换码软件 编辑:程序博客网 时间:2024/05/16 09:01
转载请注明出处!本博客地址:http://blog.csdn.net/mylzc

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

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

代码下载请按此

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

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


界面2(发送堆栈信息)



TestActivity.java

[java] view plaincopy
  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. public class TestActivity extends Activity {  
  19.     /** Called when the activity is first created. */  
  20.   
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {//给主线程设置一个处理运行时异常的handler  
  27.   
  28.             @Override  
  29.             public void 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 + "\n");//设备的Android版本号  
  39.                 sb.append("Model is ");  
  40.                 sb.append(Build.MODEL+"\n");//设备型号  
  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.             public void onClick(View v) {  
  56.                 Integer a = null;  
  57.                 a.toString();//触发nullpointer运行时错误  
  58.                   
  59.             }  
  60.         });  
  61.           
  62.     }  
  63. }  
main.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:text="@string/hello" />  
  7.     <EditText android: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.     <Button android: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>  

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