android 记录崩溃日志

2014-04-23 08:21

    每个android应用都是由一个Application和多个activity或者server构成.应用启动时,会首先启动Application.在Application的onCreate方法中调用

1 Thread.setDefaultUncaughtExceptionHandler(handler);

就可以捕获导致应用崩溃的错误信息了.

首先应用要有读写sd卡权限

1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

自定义一个Application,并在AndroidManifest.xml中使用这个Application

1 <application
2         android:name=".MyApplication">
3         ...
4 </application>
001 public class MyApplication extends Application {
002     private static final String LOG_DIR = Environment
003             .getExternalStorageDirectory().getAbsolutePath() + "/oldfeel/log/";
004     private static final String LOG_NAME = getCurrentDateString() + ".txt";
005     private ArrayList<Activity> list = new ArrayList<Activity>();
006  
007     @Override
008     public void onCreate() {
009         super.onCreate();
010         Thread.setDefaultUncaughtExceptionHandler(handler);
011     }
012  
013     UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
014  
015         @Override
016         public void uncaughtException(Thread thread, Throwable ex) {
017             writeErrorLog(ex);
018             Intent intent = new Intent(getApplicationContext(),
019                     CollapseActivity.class);
020             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
021             startActivity(intent);
022             exit();
023         }
024     };
025  
026     /**
027      * 打印错误日志
028      
029      * @param ex
030      */
031     protected void writeErrorLog(Throwable ex) {
032         String info = null;
033         ByteArrayOutputStream baos = null;
034         PrintStream printStream = null;
035         try {
036             baos = new ByteArrayOutputStream();
037             printStream = new PrintStream(baos);
038             ex.printStackTrace(printStream);
039             byte[] data = baos.toByteArray();
040             info = new String(data);
041             data = null;
042         catch (Exception e) {
043             e.printStackTrace();
044         finally {
045             try {
046                 if (printStream != null) {
047                     printStream.close();
048                 }
049                 if (baos != null) {
050                     baos.close();
051                 }
052             catch (Exception e) {
053                 e.printStackTrace();
054             }
055         }
056         Log.d("example""崩溃信息 " + info);
057         File dir = new File(LOG_DIR);
058         if (!dir.exists()) {
059             dir.mkdirs();
060         }
061         File file = new File(dir, LOG_NAME);
062         try {
063             FileOutputStream fileOutputStream = new FileOutputStream(file, true);
064             fileOutputStream.write(info.getBytes());
065             fileOutputStream.close();
066         catch (FileNotFoundException e) {
067             e.printStackTrace();
068         catch (IOException e) {
069             e.printStackTrace();
070         }
071  
072     }
073  
074     /**
075      * 获取当前日期
076      
077      * @return
078      */
079     private static String getCurrentDateString() {
080         String result = null;
081         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
082                 Locale.getDefault());
083         Date nowDate = new Date();
084         result = sdf.format(nowDate);
085         return result;
086     }
087  
088     /**
089      * Activity关闭时,删除Activity列表中的Activity对象
090      */
091     public void removeActivity(Activity a) {
092         list.remove(a);
093     }
094  
095     /**
096      * 向Activity列表中添加Activity对象
097      */
098     public void addActivity(Activity a) {
099         list.add(a);
100     }
101  
102     /**
103      * 关闭Activity列表中的所有Activity
104      */
105     public void exit() {
106         for (Activity activity : list) {
107             if (null != activity) {
108                 activity.finish();
109             }
110         }
111         // 杀死该应用进程
112         android.os.Process.killProcess(android.os.Process.myPid());
113     }
114 }

系统错误后要还是要提示用户系统错误.这个是崩溃activity,

1 <activity
2     android:name="com.example.test.CollapseActivity"
3     android:theme="@android:style/Theme.Holo.Dialog.MinWidth" >
4 </activity>
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     android:gravity="center"
06     android:orientation="horizontal" >
07  
08     <Button
09         android:id="@+id/collapse_restart"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:layout_weight="1.0"
13         android:text="重启应用" />
14  
15     <Button
16         android:id="@+id/collapse_exit"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:layout_weight="1.0"
20         android:text="退出应用" />
21  
22 </LinearLayout>
01 public class CollapseActivity extends Activity {
02     private Button btnRestart, btnExit;
03  
04     @Override
05     protected void onCreate(Bundle savedInstanceState) {
06         super.onCreate(savedInstanceState);
07         setContentView(R.layout.collapse_activity);
08         setTitle("应用崩溃了");
09         btnRestart = (Button) findViewById(R.id.collapse_restart);
10         btnExit = (Button) findViewById(R.id.collapse_exit);
11         btnRestart.setOnClickListener(new OnClickListener() {
12  
13             @Override
14             public void onClick(View v) {
15                 Intent intent = new Intent(getApplicationContext(),
16                         MainActivity.class);
17                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
18                 startActivity(intent);
19                 finish();
20             }
21         });
22         btnExit.setOnClickListener(new OnClickListener() {
23  
24             @Override
25             public void onClick(View v) {
26                 finish();
27             }
28         });
29     }
30 }


http://my.oschina.net/oldfeel/blog/187862

^