Android Open Source - FrameLite Crash Handler






From Project

Back to project page FrameLite.

License

The source code is released under:

GNU General Public License

If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.miku.framelite.services;
/*from   w w  w .j  a  v a  2  s. c o m*/
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;

import com.miku.framelite.utils.DimensionUtils;
import com.miku.framelite.utils.Log;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Environment;

/**
 * ?????????????
 * 
 * @author xr.lee
 * 
 */
public class CrashHandler implements UncaughtExceptionHandler {
  private static final String TAG = CrashHandler.class.getSimpleName();

  private Context mContext;
  private UncaughtExceptionHandler mDefaultHandler;

  private static CrashHandler INSTANCE;

  private CrashHandler(Context context) {
    mContext = context;
    mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(this);
  }

  public static void Init(Context context) {
    if (INSTANCE == null) {
      INSTANCE = new CrashHandler(context);
    }
  }

  @Override
  public void uncaughtException(Thread thread, Throwable ex) {
    handleUncaughtException(thread, ex);
    // ????????????
    if (mDefaultHandler != null) {
      mDefaultHandler.uncaughtException(thread, ex);
    }
  }

  private void handleUncaughtException(Thread thread, Throwable throwable) {
    // ??sd???????????????
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      saveCrashInfoToFile(thread, throwable);
    }
  }

  /**
   * ?????????????
   * 
   * @param thread
   *            ex
   * @return
   */
  private void saveCrashInfoToFile(Thread thread, Throwable ex) {
    Log.writeLog(thread, TAG, collectCrashDeviceInfo(), ex);
  }

  /**
   * ??????????????
   */
  public String collectCrashDeviceInfo() {
    StringBuilder sb = new StringBuilder();
    try {
      PackageManager pm = mContext.getPackageManager();
      PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
          PackageManager.GET_ACTIVITIES);
      if (pi != null) {
        sb.append("VersionName:")
            .append(pi.versionName == null ? "not set" : pi.versionName).append("\n\r");
        sb.append("VersionCode:").append(pi.versionCode).append("\n\r");
      }
      
    } catch (NameNotFoundException e) {
      Log.e(TAG, "Error while collect package info", e);
    }
    
    int[] screen=DimensionUtils.getDisplayScreenResolution(mContext);
    sb.append("ScreenInfo--Width:").append(screen[0]).append(" Height:").append(screen[1]).append("\n\r");
    
    // ?????????????????.?Build?????????????????,
    // ??: ???????,????? ???????????????
    Field[] fields = Build.class.getDeclaredFields();
    for (Field field : fields) {
      try {
        field.setAccessible(true);
        sb.append(field.getName()).append(" : ").append(field.get(null).toString())
            .append("\n\r");
      } catch (Exception e) {
        Log.e(TAG, "Error while collect crash info", e);
      }
    }
    return sb.toString();
  }

}




Java Source Code List

com.miku.framelite.FrameApplication.java
com.miku.framelite.FrameBaseActivity.java
com.miku.framelite.FrameOrmBaseActivity.java
com.miku.framelite.adapter.FrameBaseAdapter.java
com.miku.framelite.annotations.ViewInject.java
com.miku.framelite.api.BaseRequest.java
com.miku.framelite.api.IRequest.java
com.miku.framelite.api.RetResult.java
com.miku.framelite.api.core.Executor.java
com.miku.framelite.api.database.AbstractDatabaseRequest.java
com.miku.framelite.api.database.AbstractOrmDatabaseRequest.java
com.miku.framelite.api.database.DatabaseQueryRequest.java
com.miku.framelite.api.database.DatabaseType.java
com.miku.framelite.api.http.AbstractHttpRequest.java
com.miku.framelite.api.http.HttpStringGetRequest.java
com.miku.framelite.api.http.HttpStringPostRequest.java
com.miku.framelite.api.http.HttpType.java
com.miku.framelite.api.webservice.AbstractWebServiceRequest.java
com.miku.framelite.api.webservice.WebServiceConnectionSE.java
com.miku.framelite.api.webservice.WebServiceHttpTransportSE.java
com.miku.framelite.api.webservice.WebServiceJsonRequest.java
com.miku.framelite.api.webservice.WebServiceStringRequest.java
com.miku.framelite.httpx.IDownloadHandler.java
com.miku.framelite.httpx.IHttpX.java
com.miku.framelite.httpx.core.DownloadHandler.java
com.miku.framelite.httpx.core.HttpX.java
com.miku.framelite.services.CrashHandler.java
com.miku.framelite.utils.BitmapUtils.java
com.miku.framelite.utils.DateUtils.java
com.miku.framelite.utils.DimensionUtils.java
com.miku.framelite.utils.EncryptionUtils.java
com.miku.framelite.utils.HttpUtils.java
com.miku.framelite.utils.Log.java
com.miku.framelite.utils.TelePhoneUtils.java
com.miku.framelite.utils.ViewUtils.java