瀏覽代碼

1:增加崩溃日志捕获及发送功能。

guiying712 8 年之前
父節點
當前提交
f9b0e3188b

二進制
module_app/libs/acra-4.5.0.jar


+ 46 - 0
module_app/src/main/java/com/guiying/androidmodulepattern/MyApplication.java

@@ -5,6 +5,15 @@ import android.content.Context;
 import com.github.mzule.activityrouter.annotation.Modules;
 import com.guiying.common.base.BaseApplication;
 
+import org.acra.ACRA;
+import org.acra.ReportField;
+import org.acra.ReportingInteractionMode;
+import org.acra.annotation.ReportsCrashes;
+import org.acra.collector.CrashReportData;
+import org.acra.sender.EmailIntentSender;
+import org.acra.sender.ReportSender;
+import org.acra.sender.ReportSenderException;
+
 /**
  * <p>这里是整个组件化项目管理各个组件的地方,所有需要使用的组件必须在此声明</p>
  *
@@ -12,6 +21,21 @@ import com.guiying.common.base.BaseApplication;
  * @version V1.2.0
  * @name MyApplication
  */
+@ReportsCrashes(
+        mailTo = "guiying705@Gmail.com",
+        mode = ReportingInteractionMode.DIALOG,
+        customReportContent = {
+                ReportField.APP_VERSION_NAME,
+                ReportField.ANDROID_VERSION,
+                ReportField.PHONE_MODEL,
+                ReportField.CUSTOM_DATA,
+                ReportField.BRAND,
+                ReportField.STACK_TRACE,
+                ReportField.LOGCAT,
+                ReportField.USER_COMMENT},
+        resToastText = R.string.crash_toast_text,
+        resDialogText = R.string.crash_dialog_text,
+        resDialogTitle = R.string.crash_dialog_title)
 @Modules({"app", "main", "girls", "news"})
 public class MyApplication extends BaseApplication {
 
@@ -19,6 +43,10 @@ public class MyApplication extends BaseApplication {
     @Override
     public void onCreate() {
         super.onCreate();
+        //崩溃日志记录初始化
+        ACRA.init(this);
+        ACRA.getErrorReporter().removeAllReportSenders();
+        ACRA.getErrorReporter().setReportSender(new CrashReportSender());
     }
 
     @Override
@@ -27,4 +55,22 @@ public class MyApplication extends BaseApplication {
         // dex突破65535的限制
         //MultiDex.install(this);
     }
+
+
+    /**
+     * 发送崩溃日志
+     */
+    private class CrashReportSender implements ReportSender {
+        CrashReportSender() {
+            ACRA.getErrorReporter().putCustomData("PLATFORM", "ANDROID");
+            ACRA.getErrorReporter().putCustomData("BUILD_ID", android.os.Build.ID);
+            ACRA.getErrorReporter().putCustomData("DEVICE_NAME", android.os.Build.PRODUCT);
+        }
+
+        @Override
+        public void send(Context context, CrashReportData crashReportData) throws ReportSenderException {
+            EmailIntentSender emailSender = new EmailIntentSender(getApplicationContext());
+            emailSender.send(context, crashReportData);
+        }
+    }
 }

+ 6 - 0
module_app/src/main/res/values/strings.xml

@@ -3,4 +3,10 @@
 
     <string name="global_scheme">module</string>
 
+    <!-- 崩溃 -->
+    <string name="crash_toast_text">程序崩溃了</string>
+    <string name="crash_dialog_text">感谢您对我们的支持!</string>
+    <string name="crash_dialog_title">发送崩溃日志</string>
+    <string name="crash_dialog_ok_toast">发送成功</string>
+
 </resources>