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

來源:互聯網
上載者:User

由於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的專欄”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.