2025-02-17

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

當我們的的應用由於運行時異常導致Force Close的時候,可以設置主線程的UncaughtExceptionHandler,實現捕獲運行時異常的堆棧信息。同時用戶可以把堆棧信息通過發送郵件的方式反饋給我們。下面是實現的代碼:

代碼下載請按此

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

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


界面2(發送堆棧信息)

 

TestActivity.java

view plain
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
view plain
<?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> 

作者“lzc的專欄”

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *