天天看點

Android 異常資訊列印(适用于捕獲應用崩潰資訊收集)

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/qingfeng812/article/details/52448289

方法裡面用到的工具類(裝置資訊,記憶體資訊,使用者資訊,版本資訊),請自行查找;      
public static String getExceptionMsg(Context ct, Throwable ex) {
    StringBuffer sb = new StringBuffer();
    sb.append("----------------------異常資訊輸出-------------------------------------\n");
    Writer writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    ex.printStackTrace(printWriter);
    
    Throwable cause = ex.getCause();
    while (cause != null) {
        cause.printStackTrace(printWriter);
        cause = cause.getCause();
    }
    printWriter.close();
    String result = writer.toString();
    String phone = CommonUtil.getSharedPreferences(ct, "user_phone");
    String password = CommonUtil.getSharedPreferences(ct, "user_password");
    String master_ch = CommonUtil.getSharedPreferences(ct, "Master_ch");
    String company = CommonUtil.getSharedPreferences(ct, "erp_commpany");
    sb.append(result);
    sb.append("\n----------------------使用者資訊輸出-------------------------------------");
    sb.append("\n phone:" + phone);
    sb.append("\n password:" + password);
    sb.append("\n master_ch:" + master_ch);
    sb.append("\n company:" + company);
    sb.append("\n----------------------裝置資訊輸出-------------------------------------");
    //擷取裝置大小
    String deviceInfo = CommonUtil.getDeviceInfo(ct);
    System.out.println("deviceInfo=" + deviceInfo);
    sb.append("\n" + deviceInfo);
    sb.append("\n手機型号:" + CommonUtil.getDeviceModel());
    sb.append("\n手機生産廠商:" + CommonUtil.getDeviceManufacturer());
    //擷取應用程式記憶體使用情況
    sb.append("\n----------------------記憶體資訊輸出-------------------------------------\n");
    sb.append(CommonUtil.getMemory());
    //擷取應用程式的目前版本号
    sb.append("\n----------------------版本資訊輸出-------------------------------------\n");
    sb.append("\n應用版本号:" + CommonUtil.ApkVersionCode(ct));
    return sb.toString();
}      

繼續閱讀