Android uses Application to rewrite the crash (simple version) of the app)

Source: Internet
Author: User
Tags throwable


Original article: http://blog.csdn.net/lfdfhl/article/details/9714443

Mainactivity is as follows:

Package CN. testcrash; import android. app. activity; import android. OS. bundle;/*** demo Description: * use application to customize crash ** references: * 1 http://blog.csdn.net/xiaanming/article/details/9344703 * 2 http://blog.csdn.net/itachi85/article/details/9102021 */public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init () ;}// crashprivate void Init () {system. out. println (9727/0) + "");}}

Crashapplication is as follows:

Package CN. testcrash; import android. app. application; public class crashapplication extends application {@ overridepublic void oncreate () {super. oncreate (); crashhandler = crashhandler. getinstance (); // specifies the handler crashhandler for Crash. setcrashhandler (getapplicationcontext ());}}

Crashhandler is as follows:

Package cn. testcrash; import java. lang. thread. uncaughtExceptionHandler; import android. content. context; import android. OS. logoff; import android. widget. toast; public class CrashHandler implements UncaughtExceptionHandler {private Context mContext; private static CrashHandler mCrashHandler = new CrashHandler (); public static CrashHandler getInstance () {return mCrashHandler ;} /*** set the default handler that suddenly terminates when the thread does not capture exceptions. */Public void setCrashHandler (Context context) {mContext = context; Thread. setDefaultUncaughtExceptionHandler (this);}/*** call this method when a Crash occurs */@ Overridepublic void uncaughtException (Thread thread, Throwable throwable) {// Save the error log to the SD card Utils. saveInfoToSDCard (mContext, throwable); // displays the Crash information showCrashTipToast (); try {Thread. sleep (3000);} catch (Exception e) {}// exit the application System. exit (0);} private void showCrashTipToast () {new Thread (new Runnable () {@ Overridepublic void run () {Looper. prepare (); Toast. makeText (mContext, "I am very sorry", Toast. LENGTH_LONG ). show (); logoff. loop ();}}). start ();}}

Utils is as follows:

Package cn. testcrash; import java. text. simpleDateFormat; import java. util. date; import java. util. hashMap; import java. util. map; import java. util. timeZone; import android. content. context; import android. content. pm. packageInfo; import android. content. pm. packageManager; import android. content. pm. packageManager. nameNotFoundException; import android. OS. build; public class Utils {public static void saveInfoToSDCard (Context context, Throwable throwable) {HashMap <String, String> hashMap = getBaseInfo (context); StringBuilder stringBuilder = new StringBuilder (); for (Map. entry <String, String> entry: hashMap. entrySet () {String key = entry. getKey (); String value = entry. getValue (); stringBuilder. append (key ). append ("= "). append (value ). append ("\ n");} System. out. println ("stringBuilder. toString () is as follows: "+" \ n "+ stringBuilder. toString ();/*** the remaining logic is omitted */}/*** get the basic information of the device and the App */public static HashMap <String, string> getBaseInfo (Context context) {HashMap <String, String> hashMap = new HashMap <String, String> (); PackageManager packageManager = context. getPackageManager (); PackageInfo packageInfo = null; try {packageInfo = packageManager. getPackageInfo (context. getPackageName (), PackageManager. GET_ACTIVITIES);} catch (NameNotFoundException e) {e. printStackTrace ();} hashMap. put ("versionName", packageInfo. versionName); hashMap. put ("versionCode", packageInfo. versionCode + ""); hashMap. put ("MODEL", Build. MODEL + ""); hashMap. put ("SDK_INT", Build. VERSION. SDK_INT + ""); hashMap. put ("RELEASE", Build. VERSION. RELEASE + ""); hashMap. put ("PRODUCT", Build. PRODUCT + ""); return hashMap;} private String getCurrentTime () {String currentTime = ""; long currentTimeMillis = System. currentTimeMillis (); System. setProperty ("user. timezone "," Asia/Shanghai "); TimeZone timeZone = TimeZone. getTimeZone ("Asia/Shanghai"); TimeZone. setDefault (timeZone); SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss"); Date currentDate = new Date (currentTimeMillis); currentTime = simpleDateFormat. format (currentDate); System. out. println ("currentTime =" + currentTime); return currentTime ;}}

AndroidManifest. xml is as follows:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.testcrash"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="8" />    <application        android:name="cn.testcrash.CrashApplication"        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="cn.testcrash.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

Related Article

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.