程序中的异常处理(自定义UncaughtExceptionHandler)

it2026-02-16  16

1.  继承Application  重写onCreate() 方法

1 2 3 4 5 6 7 8 9 10 11 12 13 public class MyApp extends Application {         //在整个应用第一次被创建出来的时候 执行       // 在应用程序对应的进程 第一次创建出来的时候执行       @Override      public void onCreate() {          super .onCreate();          //把自定义的异常处理类设置 给主线程           MyCrashHandler myCrashHandler = MyCrashHandler.getInstance();          myCrashHandler.init(getApplicationContext());          Thread.currentThread().setUncaughtExceptionHandler(myCrashHandler);      } }

2.  修改清单文件android:label="@string/app_name" android:name="MyApp">

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android"      package = "cn.itcast.crash"      android:versionCode = "1"      android:versionName = "1.0" >         < uses-sdk android:minSdkVersion = "8" />         < application          android:icon = "@drawable/ic_launcher"          android:label = "@string/app_name" android:name = "MyApp" >          < activity              android:label = "@string/app_name"              android:name = ".CrashActivity" >              < intent-filter >                  < action android:name = "android.intent.action.MAIN" />                     < category android:name = "android.intent.category.LAUNCHER" />              </ intent-filter >          </ activity >      </ application >    </ manifest >

3. 实现UncaughtExceptionHandler 类

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 public class MyCrashHandler implements UncaughtExceptionHandler {      // 保证MyCrashHandler只有一个实例      // 2.提供一个静态的程序变量      private static MyCrashHandler myCrashHandler;      private Context context;         // 1.私有化构造方法      private MyCrashHandler() {         }         // 3.暴露出来一个静态的方法 获取myCrashHandler         public static synchronized MyCrashHandler getInstance() {          if (myCrashHandler == null ) {              myCrashHandler = new MyCrashHandler();          }          return myCrashHandler;      }         public void init(Context context) {          this .context = context;      }         // 程序发生异常的时候调用的方法      // try catch         @Override      public void uncaughtException(Thread thread, Throwable ex) {          System.out.println( "出现错误啦 哈哈" );          StringBuilder sb = new StringBuilder();          // 1.获取当前应用程序的版本号.          PackageManager pm = context.getPackageManager();          try {              PackageInfo packinfo = pm.getPackageInfo(context.getPackageName(),                      0 );              sb.append( "程序的版本号为" + packinfo.versionName);              sb.append( "\n" );                 // 2.获取手机的硬件信息.              Field[] fields = Build. class .getDeclaredFields();              for ( int i = 0 ; i < fields.length; i++) {                  // 暴力反射,获取私有的字段信息                  fields[i].setAccessible( true );                  String name = fields[i].getName();                  sb.append(name + " = " );                  String value = fields[i].get( null ).toString();                  sb.append(value);                  sb.append( "\n" );              }              // 3.获取程序错误的堆栈信息 .              StringWriter writer = new StringWriter();              PrintWriter printWriter = new PrintWriter(writer);              ex.printStackTrace(printWriter);                             String result = writer.toString();              sb.append(result);                                            System.out.println(sb.toString());                 // 4.把错误信息 提交到服务器                         } catch (Exception e) {              e.printStackTrace();          }             // 完成自杀的操作          android.os.Process.killProcess(android.os.Process.myPid());      }    }

 

 

 

 

 

 

 

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/feelbest/p/3696194.html

最新回复(0)