【ListViewJson】【com.demo.app】【AppException】源碼分析及其在工程中作用

來源:互聯網
上載者:User

標籤:android   style   blog   http   ar   io   color   os   sp   

源碼如下:

package com.demo.app;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.lang.Thread.UncaughtExceptionHandler;import java.net.ConnectException;import java.net.SocketException;import java.net.UnknownHostException;import java.util.Date;import org.apache.commons.httpclient.HttpException;import com.demo.app.common.UIHelper;import android.content.Context;import android.content.pm.PackageInfo;import android.os.Environment;import android.os.Looper;import android.widget.Toast;/** * 應用程式異常類:用於捕獲異常和提示錯誤資訊 * @version 1.0 * @created 2012-3-21 */public class AppException extends Exception implements UncaughtExceptionHandler{    private final static boolean Debug = false;//是否儲存錯誤記錄檔        /** 定義異常類型 */    public final static byte TYPE_NETWORK     = 0x01;    public final static byte TYPE_SOCKET    = 0x02;    public final static byte TYPE_HTTP_CODE    = 0x03;    public final static byte TYPE_HTTP_ERROR= 0x04;    public final static byte TYPE_XML         = 0x05;    public final static byte TYPE_IO         = 0x06;    public final static byte TYPE_RUN         = 0x07;        private byte type;    private int code;        /** 系統預設的UncaughtException處理類 */    private Thread.UncaughtExceptionHandler mDefaultHandler;        private AppException(){        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();    }        private AppException(byte type, int code, Exception excp) {        super(excp);        this.type = type;        this.code = code;                if(Debug){            this.saveErrorLog(excp);        }    }    public int getCode() {        return this.code;    }    public int getType() {        return this.type;    }            /**     * 儲存異常日誌     * @param excp     */    public void saveErrorLog(Exception excp) {        String errorlog = "errorlog.txt";        String savePath = "";        String logFilePath = "";        FileWriter fw = null;        PrintWriter pw = null;        try {            //判斷是否掛載了SD卡            String storageState = Environment.getExternalStorageState();                    if(storageState.equals(Environment.MEDIA_MOUNTED)){                savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OSChina/Log/";                File file = new File(savePath);                if(!file.exists()){                    file.mkdirs();                }                logFilePath = savePath + errorlog;            }            //沒有掛載SD卡,無法寫檔案            if(logFilePath == ""){                return;            }            File logFile = new File(logFilePath);            if (!logFile.exists()) {                logFile.createNewFile();            }            fw = new FileWriter(logFile,true);            pw = new PrintWriter(fw);            pw.println("--------------------"+(new Date().toLocaleString())+"---------------------");                excp.printStackTrace(pw);            pw.close();            fw.close();        } catch (Exception e) {            e.printStackTrace();                }finally{             if(pw != null){ pw.close(); }             if(fw != null){ try { fw.close(); } catch (IOException e) { }}        }    }        public static AppException http(int code) {        return new AppException(TYPE_HTTP_CODE, code, null);    }        public static AppException http(Exception e) {        return new AppException(TYPE_HTTP_ERROR, 0 ,e);    }    public static AppException socket(Exception e) {        return new AppException(TYPE_SOCKET, 0 ,e);    }        public static AppException io(Exception e) {        if(e instanceof UnknownHostException || e instanceof ConnectException){            return new AppException(TYPE_NETWORK, 0, e);        }        else if(e instanceof IOException){            return new AppException(TYPE_IO, 0 ,e);        }        return run(e);    }        public static AppException xml(Exception e) {        return new AppException(TYPE_XML, 0, e);    }        public static AppException network(Exception e) {        if(e instanceof UnknownHostException || e instanceof ConnectException){            return new AppException(TYPE_NETWORK, 0, e);        }        else if(e instanceof HttpException){            return http(e);        }        else if(e instanceof SocketException){            return socket(e);        }        return http(e);    }        public static AppException run(Exception e) {        return new AppException(TYPE_RUN, 0, e);    }    /**     * 擷取APP異常崩潰處理對象     * @param context     * @return     */    public static AppException getAppExceptionHandler(){        return new AppException();    }        @Override    public void uncaughtException(Thread thread, Throwable ex) {        if(!handleException(ex) && mDefaultHandler != null) {            mDefaultHandler.uncaughtException(thread, ex);        }    }    /**     * 自訂異常處理:收集錯誤資訊&發送錯誤報表     * @param ex     * @return true:處理了該異常資訊;否則返回false     */    private boolean handleException(Throwable ex) {        if(ex == null) {            return false;        }                final Context context = AppManager.getAppManager().currentActivity();                if(context == null) {            return false;        }                final String crashReport = getCrashReport(context, ex);        //顯示異常資訊&發送報告        new Thread() {            public void run() {                Looper.prepare();                Looper.loop();            }        }.start();        return true;    }    /**     * 擷取APP崩潰異常報告     * @param ex     * @return     */    private String getCrashReport(Context context, Throwable ex) {        PackageInfo pinfo = ((AppContext)context.getApplicationContext()).getPackageInfo();        StringBuffer exceptionStr = new StringBuffer();        exceptionStr.append("Version: "+pinfo.versionName+"("+pinfo.versionCode+")\n");        exceptionStr.append("Android: "+android.os.Build.VERSION.RELEASE+"("+android.os.Build.MODEL+")\n");        exceptionStr.append("Exception: "+ex.getMessage()+"\n");        StackTraceElement[] elements = ex.getStackTrace();        for (int i = 0; i < elements.length; i++) {            exceptionStr.append(elements[i].toString()+"\n");        }        return exceptionStr.toString();    }}

 

【ListViewJson】【com.demo.app】【AppException】源碼分析及其在工程中作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.