首页 > 代码库 > 自定义崩溃界面 CustomActivityOnCrash
自定义崩溃界面 CustomActivityOnCrash
项目地址:https://github.com/Ereza/CustomActivityOnCrash
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):
CustomActivityOnCrash.setLaunchErrorActivityWhenInBackground(boolean);
CustomActivityOnCrash.setShowErrorDetails(boolean);
CustomActivityOnCrash.setDefaultErrorActivityDrawable(int);
CustomActivityOnCrash.setEnableAppRestart(boolean);
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!
CustomActivityOnCrash.setEventListener(EventListener);
CustomActivityOnCrash.setRestartActivityClass(Class<? extends Activity>);
As noted, you can also use the following intent-filter to specify the restart activity:
<intent-filter>
<action android:name="cat.ereza.customactivityoncrash.RESTART" />
</intent-filter>
自定义出错时弹出的Activity
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:
<activity
android:name="cat.ereza.sample.customactivityoncrash.activity.CustomErrorActivity"
android:label="@string/error_title"
android:process=":error_activity" />
<intent-filter>
<action android:name="cat.ereza.customactivityoncrash.ERROR" />
</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:
<string name="customactivityoncrash_error_activity_error_occurred_explanation">An unexpected error occurred.</string>
<string name="customactivityoncrash_error_activity_unknown_exception">Unknown exception</string>
<string name="customactivityoncrash_error_activity_restart_app">Restart app</string>
<string name="customactivityoncrash_error_activity_close_app">Close app</string>
<string name="customactivityoncrash_error_activity_error_details">Error details</string>
<string name="customactivityoncrash_error_activity_error_details_title">Error details</string>
<string name="customactivityoncrash_error_activity_error_details_close">Close</string>
<string name="customactivityoncrash_error_activity_error_details_copy">Copy to clipboard</string>
<string name="customactivityoncrash_error_activity_error_details_copied">Copied to clipboard</string>
<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:
CustomActivityOnCrash.getStackTraceFromIntent(getIntent());
CustomActivityOnCrash.getAllErrorDetailsFromIntent(getIntent());
CustomActivityOnCrash.getRestartActivityClassFromIntent(getIntent());
CustomActivityOnCrash.getEventListenerFromIntent(getIntent());
CustomActivityOnCrash.restartApplicationWithIntent(activity, intent, eventListener);
CustomActivityOnCrash.closeApplication(activity, eventListener);
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.
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。