Crashcustomactivity (i)

Source: Internet
Author: User

Translated from the GitHub project Crashcustomactivity thank the author for their contributions, I only translated, there are small changes, do not understand or feel the right to the Comrade welcome message, Thank you.

Custom App Crash interface

This project is used to pop up a custom activity instead of a "sorry, the app has an unknown error" dialog box when an exception occurs in the application

One, how to use 1. Adding dependent libraries

Add the following dependent libraries to the Build.gradle file in your project

{    compile ‘cat.ereza:customactivityoncrash:1.3.0‘}

Of course, you can do these things manually, first download the source code, and then the library file as a library module file to import your project, and add dependencies.

Second, set up your project

First, rewrite the application, and ensure that the application class that has been configured in Androidmanifest has been replaced with the application class of its own definition.
Do the following in the application OnCreate method

@Override    publicvoidonCreate() {        super.onCreate();        //Install CustomActivityOnCrash        CustomActivityOnCrash.install(this);        //Now initialize your error handlers as normal        //i.e., ACRA.init(this);        //or Crashlytics.start(this);    }

Note: If you already have a ACRA, Crash Analysis tool, or other similar tool in your project, he can still use it normally. However, Cunstomactivitycrash must be initialized first, otherwise it will cause other tools to be unusable.

Test

You can crash your application directly with the following code

//当然了 这样写你的程序很可能会报错//就不多解释了 看一下提示就可以解决了thrownew RuntimeException("Boom");

After you run the program, an activity with an error prompt will pop up instead of a system dialog box

Options: Custom Use custom crash behavior

You can call these methods at any time to define what happens when an exception occurs, and, of course, we usually install(this) call him before the method.

1
CustomActivityOnCrash.setLaunchErrorActivityWhenInBackground(boolean);

The above method is to define whether an error also occurs when your app is running in the background, such as an error warning interface. But if, in the case of api<14, we cannot determine whether the app is in the background or the foreground, then he will always default to true, which means that the error message will always pop up. If we set to False, then the application in the background error will not have an error warning interface, there will be no System error dialog box. This method defaults to True, which is the display.

2
CustomActivityOnCrash.setShowErrorDetails(boolean);

This method is used to define whether the error warning page appears with a button to display the error details, set to True will have such a button, select False, does not appear. The default is true.

3
CustomActivityOnCrash.setEnableAppRestart(boolean);

This method to define the error warning screen must have a button to restart the app or close the app button, set to True will display the Restart App button, otherwise show the button to close the app. The default is true.
Note: If you specify a class that restarts the app or if the specified class is not found, the app will be forced to close.

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

This method to set the user to click on the Restart App button after the activity opened, if you do not call this method, then Customeractivityoncrash will be in the Androidmanifest.xml file to find the first filter ( Intent-fifter) with the "Cat.ereza.customactivityoncrash.RESTART" Action (action) to start it. If you do not find his qualifying activity, the Restart App button will turn off the app button, even if you set the Restart App button to appear.

<intent-filter>    <!-- ... -->    <action android:name="cat.ereza.customactivityoncrash.RESTART" /></intent-filter>
5
CustomActivityOnCrash.setErrorActivityClass(Class<? extends Activity>);

This method lets us define the activity that appears on the wrong interface, not the one that is now the default. If you do not set or set to NULL, then Customactivityoncrash will find the filter in the Manifest.xml file (intent-filfter) with " Cat.ereza.customactivityoncrash.ERROR "Action" activity, if no qualifying activity exists, then the default activity will be used. If you are using custom activity, then you must declare the activity's process property as ": Error_activity" in the Androidmanifest.xml file.
Example:

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

Description, you can use this filter in your custom error interface (Intent-filfter)

<intent-filter>    <!-- ... -->    <action android:name="cat.ereza.customactivityoncrash.ERROR" /></intent-filter>
Customizing the default activity

Not finished ... Cond

Crashcustomactivity (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.