码迷,mamicode.com
首页 > 移动开发 > 详细

androidcarsh

时间:2017-12-17 22:27:08      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:rup   format   shm   sdcard   swa   als   设备   简单   min   

package com.oval.cft;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import com.oval.cft.constant.OvalConstant;

import android.annotation.SuppressLint;
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;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

/**
* 自定义系统的Crash捕捉类,用Toast替换系统的对话框
* 将软件版本信息,设备信息,出错信息保存在SD卡中,可以上传到服务器中
* @author Jackyin
*
*/
public class CustomCrashHandler implements UncaughtExceptionHandler {
private static final String TAG = "Activity";
private Context mContext;
@SuppressWarnings("unused")
private static final String SDCARD_ROOT = Environment.getExternalStorageDirectory().toString();
private static CustomCrashHandler mInstance = new CustomCrashHandler();

private CustomCrashHandler() {
}

/**
* 单例模式,保证只有一个CustomCrashHandler实例存在
* @return
*/
public static CustomCrashHandler getInstance() {
return mInstance;
}

/**
* 异常发生时,系统回调的函数,我们在这里处理一些操作
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// 将一些信息保存到SDcard中
savaInfoToSD(mContext, ex);
// 提示用户程序即将退出
showToast(mContext, "很抱歉,程序遭遇异常,即将退出!");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// android.os.Process.killProcess(android.os.Process.myPid());
// System.exit(1);
ExitAppUtils.getInstance().exit();

}

/**
* 为我们的应用程序设置自定义Crash处理
*/
public void setCustomCrashHanler(Context context) {
mContext = context;
Thread.setDefaultUncaughtExceptionHandler(this);
}

/**
* 显示提示信息,需要在线程中显示Toast
* @param context
* @param msg
*/
private void showToast(final Context context, final String msg) {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
Looper.loop();
}
}).start();
}

/**
* 获取一些简单的信息,软件版本,手机版本,型号等信息存放在HashMap中
* @param context
* @return
*/
private HashMap<String, String> obtainSimpleInfo(Context context) {
HashMap<String, String> map = new HashMap<String, String>();
PackageManager mPackageManager = context.getPackageManager();
PackageInfo mPackageInfo = null;
try {
mPackageInfo = mPackageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
} catch (NameNotFoundException e) {
e.printStackTrace();
}

map.put("versionName", mPackageInfo.versionName);
map.put("versionCode", "" + mPackageInfo.versionCode);

map.put("MODEL", "" + Build.MODEL);
map.put("SDK_INT", "" + Build.VERSION.SDK_INT);
map.put("PRODUCT", "" + Build.PRODUCT);

return map;
}

/**
* 获取系统未捕捉的错误信息
* @param throwable
* @return
*/
private String obtainExceptionInfo(Throwable throwable) {
StringWriter mStringWriter = new StringWriter();
PrintWriter mPrintWriter = new PrintWriter(mStringWriter);
throwable.printStackTrace(mPrintWriter);
mPrintWriter.close();

Log.e(TAG, mStringWriter.toString());
return mStringWriter.toString();
}

/**
* 保存获取的 软件信息,设备信息和出错信息保存在SDcard中
* @param context
* @param ex
* @return
*/
private String savaInfoToSD(Context context, Throwable ex) {
String fileName = null;
StringBuffer sb = new StringBuffer();

for (Map.Entry<String, String> entry : obtainSimpleInfo(context).entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
sb.append(key).append(" = ").append(value).append("\n");
}

sb.append(obtainExceptionInfo(ex));

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File dir = new File(OvalConstant.APP_EXCEPTION_DIRECTORY);
if (!dir.exists()) {
dir.mkdir();
}

try {
fileName = dir.toString() + File.separator + paserTime(System.currentTimeMillis()) + ".log";
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(sb.toString().getBytes());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

return fileName;

}

/**
* 将毫秒数转换成yyyy-MM-dd-HH-mm-ss的格式
* @param milliseconds
* @return
*/
@SuppressLint("SimpleDateFormat")
private String paserTime(long milliseconds) {
System.setProperty("user.timezone", "Asia/Shanghai");
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
TimeZone.setDefault(tz);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String times = format.format(new Date(milliseconds));
return times;
}
}


@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressWarnings("unused")
@Override
public void onCreate() {
if (OvalConstant.Config.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
}
super.onCreate();
// 初始化应用异常处理
initCustomCrashHandler();
}
找到一个Android的异常处理的办法,经过测试可以使用,加到项目中。

androidcarsh

标签:rup   format   shm   sdcard   swa   als   设备   简单   min   

原文地址:http://www.cnblogs.com/wcLT/p/8053093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!