The inner workings are based on ACRA's dialog reporting mode:https://github.com/ACRA/acra 

第一次见到就感觉大有用途:
  • 可以在app崩溃的时候显示自定义的页面,对用户来说非常友好
  • 可以非常方便用户再次启动我们的app,或者直接在崩溃后重启
  • 还可以定制重启动后的Activity
  • 还有一个隐藏的大功能:可以收集崩溃日志,非常方便我们调试。比如测试同事在测试时,有时候很难重现那些崩溃的bug,而这个库可以非常方便的把崩溃日志提取出来,这样对于我们定位bug大有裨益!

自定义设置
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//Install CustomActivityOnCrash
CustomActivityOnCrash.install(this);
CustomActivityOnCrash.setLaunchErrorActivityWhenInBackground(true);
CustomActivityOnCrash.setShowErrorDetails(true);
CustomActivityOnCrash.setDefaultErrorActivityDrawable(R.mipmap.ic_launcher);
CustomActivityOnCrash.setEnableAppRestart(true);
//The EventListener you provide can not be an anonymous or non-static inner class
CustomActivityOnCrash.setEventListener(new MyEventListener());
CustomActivityOnCrash.setRestartActivityClass(Activity2.class);
//CustomActivityOnCrash.setErrorActivityClass(Activity2.class);

//Now initialize your error handlers as normal. i.e., ACRA.init(this);or Fabric.with(this, new Crashlytics())
}

private static class MyEventListener implements CustomActivityOnCrash.EventListener {
@Override
public void onLaunchErrorActivity() {
Log.i("bqt", "onLaunchErrorActivity");
}
@Override
public void onRestartAppFromErrorActivity() {
Log.i("bqt", "onRestartAppFromErrorActivity");
}
@Override
public void onCloseAppFromErrorActivity() {
Log.i("bqt", "onCloseAppFromErrorActivity");
}
}
}

How to use

1. Add a dependency
Add the following dependency to your build.gradle:
dependencies {
    compile 'cat.ereza:customactivityoncrash:1.5.0'
}
You can also do it manually, by downloading the source code, importing the library folder as an Android Library Module, and adding a dependency on your project to that module.

2. Set up your application
On your application class, use this snippet:

    @Override
    public void onCreate() {
        super.onCreate();

        //Install CustomActivityOnCrash
        CustomActivityOnCrash.install(this);

        //Now initialize your error handlers as normal
        //i.e., ACRA.init(this); or Fabric.with(this, new Crashlytics())
    }
WARNING! If you already have ACRA, Crashlytics or any similar library in your app, it will still work as normal, but the CustomActivityOnCrash initialization MUST be done first, or the original reporting tool will stop working.

3. Test it
Make the app crash by using something like this in your code:
throw new RuntimeException("Boom!");
The error activity should show up, instead of the system dialog.

Optional: Customization

Custom behavior

You can call the following methods at any time to customize how the library works, although usually you will call them before calling install(context):

  1. CustomActivityOnCrash.setLaunchErrorActivityWhenInBackground(boolean);

This method defines if the error activity should be launched when the app crashes while on background. By default, this is true. On API<14, it's always true since there is no way to detect查明 if the app is in foreground. If you set it to false, a crash while in background won't launch the error activity nor the system dialog, so it will be a silent crash. The default is true.


  1. CustomActivityOnCrash.setShowErrorDetails(boolean);

This method defines if the error activity must show a button with error details. If you set it to false, the button on the default error activity will disappear, thus disabling the user from seeing the stack trace. The default is true.


  1. CustomActivityOnCrash.setDefaultErrorActivityDrawable(int);

This method allows changing the default upside-down颠倒的 bug image with an image of your choice. You may pass a resource id for a drawable or a mipmap. The default is R.drawable.customactivityoncrash_error_image.


  1. CustomActivityOnCrash.setEnableAppRestart(boolean);

This method defines if the error activity must show a "Restart app" button or a "Close app" button. If you set it to false, the button on the default error activity will close the app instead of restarting. The default is true.

Warning! If you set it to true, there is the possibility of it still displaying the "Close app" button, if no restart activity is specified or found! 

  1. CustomActivityOnCrash.setEventListener(EventListener);

This method allows you to specify an event listener in order to get notified when the library shows the error activity, restarts or closes the app. The EventListener you provide can not be an anonymous or non-static inner class, because it needs to be serialized连载 by the library. The library will throw an exception if you try to set an invalid class. If you set it to null, no event listener will be invoked. The default is null.


  1. CustomActivityOnCrash.setRestartActivityClass(Class<? extends Activity>);

This method sets the activity that must be launched by the error activity when the user presses the button to restart the app. If you don't set it (or set it to null), the library will use the first activity on your manifest that has an intent-filter with action cat.ereza.customactivityoncrash.RESTART, and if there is none, the default launchable activity on your app. If no launchable activity can be found and you didn't specify any, the "restart app" button will become a "close app" button, even if setEnableAppRestart is set to true.


As noted, you can also use the following intent-filter to specify the restart activity:
  1. <intent-filter>
  2.     <action android:name="cat.ereza.customactivityoncrash.RESTART" />
  3. </intent-filter>

自定义出错时弹出的Activity
  1. CustomActivityOnCrash.setErrorActivityClass(Class<? extends Activity>);
This method allows you to set a custom error activity to be launched, instead of the default one. Use it if you need further customization that is not just strings, colors or themes (see below). If you don't set it (or set it to null), the library will use the first activity on your manifest that has an intent-filter with action cat.ereza.customactivityoncrash.ERROR, and if there is none, a default error activity from the library. If you use this, the activity must be declared in your AndroidManifest.xml, with process set to: error_activity.

Example:
  1. <activity
  2.     android:name="cat.ereza.sample.customactivityoncrash.activity.CustomErrorActivity"
  3.     android:label="@string/error_title"
  4.     android:process=":error_activity" />

As noted, you can also use the following intent-filter to specify the error activity:

  1. <intent-filter>
  2.     <action android:name="cat.ereza.customactivityoncrash.ERROR" />
  3. </intent-filter>

Customization of the default activity

You can override several resources to customize the default activity:

Theme:
You can override the default error activity theme by defining a theme in your app with the following id: CustomActivityOnCrashTheme

Image:
By default, an image of a bug is displayed. You can change it to any image by creating a customactivityoncrash_error_image drawable on all density buckets (mdpi, hdpi, xhdpi, xxhdpi and xxxhdpi). You can also use the provided CustomActivityOnCrash.setDefaultErrorActivityDrawable(int) method.

Strings:
You can provide new strings and translations for the default error activity strings by overriding the following strings:
  1. <string name="customactivityoncrash_error_activity_error_occurred_explanation">An unexpected error occurred.</string>
  2. <string name="customactivityoncrash_error_activity_unknown_exception">Unknown exception</string>
  3. <string name="customactivityoncrash_error_activity_restart_app">Restart app</string>
  4. <string name="customactivityoncrash_error_activity_close_app">Close app</string>
  5. <string name="customactivityoncrash_error_activity_error_details">Error details</string>
  6. <string name="customactivityoncrash_error_activity_error_details_title">Error details</string>
  7. <string name="customactivityoncrash_error_activity_error_details_close">Close</string>
  8. <string name="customactivityoncrash_error_activity_error_details_copy">Copy to clipboard</string>
  9. <string name="customactivityoncrash_error_activity_error_details_copied">Copied to clipboard</string>
  10. <string name="customactivityoncrash_error_activity_error_details_clipboard_label">Error information</string>

There is a sample project module with examples of these overrides. If in doubt存在疑问, check the code in that module.

Completely custom error activity

If you choose to create your own completely custom error activity, you can use these methods:

  1. CustomActivityOnCrash.getStackTraceFromIntent(getIntent());

Returns the stack trace that caused the error as a string.


  1. CustomActivityOnCrash.getAllErrorDetailsFromIntent(getIntent());

Returns several error details including the stack trace that caused the error, as a string. This is used in the default error activity error details dialog.


  1. CustomActivityOnCrash.getRestartActivityClassFromIntent(getIntent());

Returns the class of the activity you have to launch to restart the app, or null if not set.


  1. CustomActivityOnCrash.getEventListenerFromIntent(getIntent());

Returns the event listener that you must pass to restartApplicationWithIntent(activity, intent, eventListener) or closeApplication(activity, eventListener).


  1. CustomActivityOnCrash.restartApplicationWithIntent(activity, intent, eventListener);

Kills the current process and restarts the app again with an startActivity() to the passed intent. You MUST call this to restart the app, or you will end up having several Application class instances and experience multiprocess issues in API<17.


  1. CustomActivityOnCrash.closeApplication(activity, eventListener);

Closes the app and kills the current process. You MUST call this to close the app, or you will end up having several Application class instances and experience multiprocess issues in API<17.


The sample project module includes an example of a custom error activity. If in doubt, check the code in that module.

Using Proguard? 混淆

No need to add special rules, the library should work even with即使 obfuscation混淆.

Inner workings 工作原理

This library relies on依靠 the Thread.setDefaultUncaughtExceptionHandler method. When an exception is caught by the library's UncaughtExceptionHandler it does the following:
  • Captures捕获 the stack trace that caused the crash
  • Launches a new intent to the error activity passing the stacktrace as an extra.
  • Kills the current process.

The inner workings are based on ACRA's dialog reporting mode with some minor tweaks小的调整. Look at the code if you need more detail about how it works.


Incompatibilities 兼容性

  • CustomActivityOnCrash will not work in these cases:
    • With any custom UncaughtExceptionHandler set after initializing初始化 the library, that does not call back to the original handler.
    • With ACRA enabled and reporting mode set to TOAST or DIALOG.
  • If you use a custom UncaughtExceptionHandler, it will not be called if you initialize it before the library initialization (so, Crashlytics or ACRA initialization must be done after CustomActivityOnCrash initialization).
  • On some rare特殊的 cases on devices with API<14, the app may enter a restart loop when a crash occurs. Therefore, using it on API<14 is not recommended.
  • If your app initialization or error activity crash, there is a possibility of entering an infinite无限的 restart loop (this is checked by the library for the most common cases, but could happen in rarer cases).
  • The library has not been tested with multidex enabled. It uses Class.forName() to load classes, so maybe that could cause some problem in API<21. If you test it with such configuration, please provide feedback反馈!
  • The library has not been tested with multiprocess多进程 apps. If you test it with such configuration, please provide feedback too!

Disclaimers 免责声明

  • This will not avoid ANRs from happening.
  • This will not catch native errors.
  • There is no guarantee保证 that this will work on every device.
  • This library will not make you toast for breakfast早餐 :)  这估计是老美的冷笑话

Contributing & license

Any contribution in order to make this library better will be welcome!
The library is licensed under the Apache License 2.0.

自定义崩溃界面 CustomActivityOnCrash的更多相关文章

  1. 以前编写的inno setup脚本,涵盖了自定义安装界面,调用dll等等应用 (转)

    以前编写的inno setup脚本,涵盖了自定义安装界面,调用dll等等应用 (转) ; Script generated by the Inno Setup 脚本向导. ; SEE THE DOCU ...

  2. Wix 安装部署教程(三)自定义安装界面和行为

    接上一篇自定义安装界面,这篇继续探索,首先介绍下,Wix为我们定义了五种风格,每种风格的UI都是有一定顺序的.我们可以改变安装顺序,也可以完全自定义一个Dialog插入其中.比如Wix_Mondo 风 ...

  3. linux启动后自动登录并运行自定义图形界面程序

    在<Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法>一文中提到linux启动在以后运行一个独占显示器的图形程序的两种办法. 1.不启动xserver,使 ...

  4. iOS开发——UI_swift篇&TableView自定义聊天界面

    TableView自定义聊天界面   1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 ...

  5. InstallShield自定义安装界面

    原文:InstallShield自定义安装界面 版权声明: 可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息. 前言: 对于一些InstallShield用户或企业,对于安装包界面除了 ...

  6. swift3.0自定义相机界面

    这是公司上上上一个项目的自定义相机界面,原来是swift2.0写的,今天改为swift3.0,记录一下. 效果图如下:                                         ...

  7. andriod 自定义来电界面功能

    由于近期所做一个项目需要做类似于“来电秀”的功能,所以上网搜索了一些相关资料,加上自己的一些想法,做了一个Demo.一下是我对该功能实现的一些想法,不对的地方欢迎各位指出.最后我会给出Demo 的源代 ...

  8. FineReport中如何自定义登录界面

    在登录平台时,不希望使用FR默认的内置登录界面,想通过自定义登录界面实现登录操作,内置登录界面如下图: 登录界面,获取到用户名和密码的值,发送到报表系统,报表服务带着这两个参数访问认证地址进行认证. ...

  9. ThinkPHP自定义成功界面、失败界面、异常界面

    在ThinkPHP的手册中,附录里边的配置参考,有一个模板引擎设置. 或者在手册里面的控制器,跳转和重定向里面. 紧接着,就讲到了如何自定义这些界面. 将上诉的配置参数写到到配置文件里,修改路径到自己 ...

随机推荐

  1. Xcode10升级项目报错library not found for -lstdc++.6.0.9

    在升级Xcode10后运行项目会发出报了一个错“library not found for -libstdc++.6.0.9”,很简单,就是因为xocde10后这个libstd++.6.0.9库已经不 ...

  2. ref:Java安全之反序列化漏洞分析(简单-朴实)

    ref:https://mp.weixin.qq.com/s?__biz=MzIzMzgxOTQ5NA==&mid=2247484200&idx=1&sn=8f3201f44e ...

  3. EOJ 3247 铁路修复计划

    二分,最小生成树. 二分一下$k$,然后每次算最小生成树验证即可,事实证明,$cmp$函数,参数用引用还是能提高效率的,不引用一直$TLE$,时限有点卡常. 然后错误的代码好像$AC$了啊,$L$和$ ...

  4. FastReport.Net使用:[38]关系的使用

    打印所有成绩 1. 数据源准备 接下来我们需要打印学生成绩,而成绩表中无姓名,我们通过建立Realtion关系来打印数据. 2. 创建Relation关系 在数据视图上的动作下拉菜单中选择“新建关系” ...

  5. 【51Nod 1190】最小公倍数之和 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1190 \[ \begin{aligned} &\sum_{i=a ...

  6. codevs 1226 倒水问题

    1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x, ...

  7. Tsinsen 拉拉队排练

    建回文树,然后判断长度奇偶性,统计下来排序即可. 题目链接:http://www.tsinsen.com/ViewGProblem.page?gpid=A1255 By:大奕哥 #include< ...

  8. WordPress插件会员简化1.58 -任意文件下载漏洞(附poc)

    今天我们将讨论在WordPress插件WordPress插件与重点会员简化v1.58作为这个剧本的创作时间不打补丁的扶贫开发实践.脆弱脚本如下: CVE-ID:cve-2017-1002008 当然, ...

  9. 精通android体系架构、mvc、常见的设计模式、控制反转(ioc)

    1.请看某个著名的it公司一则招聘信息的其中一条要求:“熟悉android系统架构及相关技术,1年以上实际android平台开发经验:”,里面非常明确的说道要求熟练android系统架构,这从某种程度 ...

  10. JSON 与 JS 对象的关系

    很多人搞不清楚 JSON 和 Js 对象的关系,甚至连谁是谁都不清楚.简单来说: JSON 是 JS 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串. 如 var obj ...