小白装逼:崩溃信息ACRA

xiaoxiao2021-02-28  76

由于本人小白一个,所以在项目中会经常出现程序崩溃的情况,要是在调试的时候出现还好,可以直接看报错的信息,但是有时候不在调试的情况下,或者给别人测试的情况下,出现崩溃但又不知道其原因,这对程序的维护就增加的很多困难,所以我在项目中使用了ACRA的崩溃反馈框架,可以说贼好用~~.

导入包

compile 'ch.acra:acra:4.9.0'

ACRA的配置及使用

ACRA的使用很简单: 1.在Application中设置模式及初始化 2.创建CrashSenderFactory,MyKeyStoreFactory类,用于触发崩溃后调用

在Application中设置模式及初始化

manifest中添加设置 App的类名可以自定义的

<application android:name=".App" </application> @ReportsCrashes( //设置模式,SILENT:不提示,直接进行处理;;NOTIFICATION:在通知栏中显示提示;;TOAST:使用toast显示提示;;DIALOG:使用一个对话框来提示 mode = ReportingInteractionMode.TOAST, 这里需要在res资源的string.xml中定义好提示的文字(Toast模式下) resToastText = R.string.app_error, // 更换默认的发送器 reportSenderFactoryClasses = {xxx.CrashSenderFactory.class} //xxx为包名 ) //必须在一个继承Application且在manifest中声明的类中设置、初始化 public class App extends Application{ @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); ACRA.init(this); } }

创建CrashSenderFactory,MyKeyStoreFactory类

CrashSenderFactory类是用来替换默认的发送器的(需要继承ReportSenderFactory ),在程序崩溃的时候会触发这个类的create方法,并返回ReportSender (内部为对崩溃信息的处理)

public class CrashSenderFactory implements ReportSenderFactory { /** * @param context Application context. * @param config Configuration to use when sending reports. * @return Fully configured instance of the relevant ReportSender. */ @NonNull @Override public ReportSender create(@NonNull Context context, @NonNull ACRAConfiguration config) { return new MyKeyStoreFactory(); } }

MyKeyStoreFactory 中的send方法是对崩溃信息的处理,在这里可以将崩溃信息发到服务器或保存在本地

public class MyKeyStoreFactory implements ReportSender { /** * Send crash report data. * <p> * Method will be called from the {@link SenderService}. * * @param context Android Context in which to send the crash report. * @param errorContent Stores key/value pairs for each report field. * A report field is identified by a {@link ReportField} enum value. * @throws ReportSenderException If anything goes fatally wrong during the handling of crash * data, you can (should) throw a {@link ReportSenderException} * with a custom message. */ @Override public void send(@NonNull Context context, @NonNull CrashReportData errorContent) throws ReportSenderException { //errorContent内包含了崩溃的信息及此设备的信息 String content =errorContent.getProperty(ReportField.BUILD)+"\n" +errorContent.getProperty(ReportField.DUMPSYS_MEMINFO)+"\n" +errorContent.getProperty(ReportField.STACK_TRACE)+"\n"; Log.e("---err---","崩溃信息:"+content); } }

errorContent中的崩溃信息标识在ReportField类中有注释,这里就不把源码粘贴出来了,直接将一些常用的拿出来,方便大家使用(具体的可以到源码中看)

PP_VERSION_CODE:应用程序的版本号;; APP_VERSION_NAME:应用程序版本名;; ANDROID_VERSION:使用的安卓版本;; BUILD:Android构建细节;; BRAND:Device brand (manufacturer or carrier),设备品牌(制造商或运营商);; DUMPSYS_MEMINFO:应用程序进程的内存状态的细节;; DUMPSYS_MEMINFO:堆栈跟踪的信息;;

结尾吐槽

以上是本人项目中接触到的对程序崩溃信息收集的方法.

以上内容,纯属个人理解,如有错误,请指出纠正,若有雷同,你肯定在做梦~~~~

转载请注明原文地址: https://www.6miu.com/read-54892.html

最新回复(0)