Android小技巧(一):實現捕獲應用的運行時異常

轉載請註明出處!本博客地址:http://blog.csdn.net/mylzchtml

因爲Android設備各異,第三方定製的Android系統也很是多,咱們不可能對全部的設備場景都進行測試,於是開發一款徹底無bug的應用幾乎是不可能的任務,那麼當應用在用戶的設備上Force Close時,咱們是否是能夠捕獲這個錯誤,記錄用戶的設備信息,而後讓用戶選擇是否反饋這些堆棧信息,經過這種bug反饋方式,咱們能夠有針對性地對bug進行修復。java

當咱們的的應用因爲運行時異常致使Force Close的時候,能夠設置主線程的UncaughtExceptionHandler,實現捕獲運行時異常的堆棧信息。同時用戶能夠把堆棧信息經過發送郵件的方式反饋給咱們。下面是實現的代碼:android

代碼下載請按此app

例子:點擊按鈕後,會觸發一個NullPointerException的運行時異常,這個例子實現了捕獲運行時異常,發送郵件反饋設備和堆棧信息的功能。ide

界面1(觸發運行時異常)測試

界面2(發送堆棧信息)ui

 

TestActivity.javaspa

 

  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 + "\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.             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 + "\n");//設備的Android版本號
				sb.append("Model is ");
				sb.append(Build.MODEL+"\n");//設備型號
				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.net

 

 

  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: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" />
	<EditText android:id="@+id/editText1" android:layout_width="match_parent"
		android:text="點擊按鈕觸發運行時異常" android:layout_height="wrap_content"
		android:layout_weight="1" android:gravity="top"></EditText>
	<Button android:text="按鈕" android:id="@+id/button"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_gravity="center_horizontal"></Button>
</LinearLayout>

轉載請註明出處!本博客地址:http://blog.csdn.net/mylzc線程

相關文章
相關標籤/搜索