|
1 | 1 | 开发中异常的处理
|
2 | 2 | ===
|
3 | 3 |
|
| 4 | + |
| 5 | + |
4 | 6 | 1. 实现未捕捉异常处理器
|
5 |
| - ```java |
6 |
| - public class MyExceptionHandler implements UncaughtExceptionHandler { |
7 |
| - private static final String TAG = "MyExceptionHandler"; |
8 |
| - @Override |
9 |
| - public void uncaughtException(Thread arg0, Throwable arg1) { |
10 |
| - Logger.i(TAG, "发生了异常,但是被哥捕获了..."); |
11 |
| - try { |
12 |
| - Field[] fields = Build.class.getDeclaredFields();//可以通过Build的属性来获取到手机的硬件信息,由于不同手机的硬件信息部一定有,所以要用反射得到 |
13 |
| - StringBuffer sb = new StringBuffer(); |
14 |
| - for(Field field: fields){ |
15 |
| - String info = field.getName()+ ":"+field.get(null)+"\n"; |
16 |
| - sb.append(info); |
17 |
| - } |
18 |
| - StringWriter sw = new StringWriter(); |
19 |
| - PrintWriter pw = new PrintWriter(sw); |
20 |
| - arg1.printStackTrace(pw);//通过这个来得到异常信息 |
21 |
| - String errorlog = sw.toString(); |
22 |
| - |
23 |
| - File file = new File(Environment.getExternalStorageDirectory(), |
24 |
| - "error.log"); |
25 |
| - FileOutputStream fos = new FileOutputStream(file); |
26 |
| - sb.append(errorlog); |
27 |
| - fos.write(sb.toString().getBytes()); |
28 |
| - fos.close(); |
29 |
| - } catch (Exception e) { |
30 |
| - e.printStackTrace(); |
31 |
| - } |
32 |
| - android.os.Process.killProcess(android.os.Process.myPid());//这个是只能杀死自己不能杀死别人,这时候系统发现程序在自己的范围之内死了, |
33 |
| - 系统就会重启程序到出现错误之前的那个Activity。 |
| 7 | + |
| 8 | +```java |
| 9 | +public class MyExceptionHandler implements UncaughtExceptionHandler { |
| 10 | + private static final String TAG = "MyExceptionHandler"; |
| 11 | + @Override |
| 12 | + public void uncaughtException(Thread arg0, Throwable arg1) { |
| 13 | + Logger.i(TAG, "发生了异常,但是被哥捕获了..."); |
| 14 | + try { |
| 15 | + Field[] fields = Build.class.getDeclaredFields();//可以通过Build的属性来获取到手机的硬件信息,由于不同手机的硬件信息部一定有,所以要用反射得到 |
| 16 | + StringBuffer sb = new StringBuffer(); |
| 17 | + for(Field field: fields){ |
| 18 | + String info = field.getName()+ ":"+field.get(null)+"\n"; |
| 19 | + sb.append(info); |
| 20 | + } |
| 21 | + StringWriter sw = new StringWriter(); |
| 22 | + PrintWriter pw = new PrintWriter(sw); |
| 23 | + arg1.printStackTrace(pw);//通过这个来得到异常信息 |
| 24 | + String errorlog = sw.toString(); |
| 25 | + |
| 26 | + File file = new File(Environment.getExternalStorageDirectory(), |
| 27 | + "error.log"); |
| 28 | + FileOutputStream fos = new FileOutputStream(file); |
| 29 | + sb.append(errorlog); |
| 30 | + fos.write(sb.toString().getBytes()); |
| 31 | + fos.close(); |
| 32 | + } catch (Exception e) { |
| 33 | + e.printStackTrace(); |
34 | 34 | }
|
| 35 | + android.os.Process.killProcess(android.os.Process.myPid());//这个是只能杀死自己不能杀死别人,这时候系统发现程序在自己的范围之内死了, |
| 36 | + 系统就会重启程序到出现错误之前的那个Activity。 |
35 | 37 | }
|
36 |
| - ``` |
| 38 | +} |
| 39 | +``` |
37 | 40 |
|
38 | 41 | 2. 让这个处理器生效
|
39 |
| - ```java |
40 |
| - /** |
41 |
| - * 代表的是当前应用程序的进程. |
42 |
| - */ |
43 |
| - public class MobliesafeApplication extends Application { |
44 |
| - public BlackNumberInfo info; |
45 |
| - |
46 |
| - @Override |
47 |
| - public void onCreate() { |
48 |
| - super.onCreate(); |
49 |
| - Thread.currentThread().setUncaughtExceptionHandler(new MyExceptionHandler());//这样就能够让异常的处理器设置到我们的程序中 |
50 |
| - } |
| 42 | + |
| 43 | +```java |
| 44 | +/** |
| 45 | + * 代表的是当前应用程序的进程. |
| 46 | + */ |
| 47 | +public class MobliesafeApplication extends Application { |
| 48 | + public BlackNumberInfo info; |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onCreate() { |
| 52 | + super.onCreate(); |
| 53 | + Thread.currentThread().setUncaughtExceptionHandler(new MyExceptionHandler());//这样就能够让异常的处理器设置到我们的程序中 |
51 | 54 | }
|
52 |
| - ``` |
| 55 | +} |
| 56 | +``` |
53 | 57 |
|
54 | 58 | ---
|
55 | 59 |
|
|
0 commit comments