在Applation 中的代码
import android.app.Application;
/**
* 1.类的用途
* 2.@author1
* 3.@data2017/10/4 22:42
*/
public class Applation extends Application {
private final static float HEAP_UTILIZATION = 0.75f;
private final static int MIN_HEAP_SIZE = 6* 1024* 1024 ;
@Override
public void onCreate() {
super.onCreate();
// 异常处理,不需要处理时注释掉这两句即可!
CrashHandler crashHandler = CrashHandler.getInstance();
// 注册crashHandler
crashHandler.init(getApplicationContext());
LogToFile.init(getApplicationContext());
}
}
在CrashHandler 中的代码
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Looper;
import android.text.format.Time;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Properties;
import java.util.TreeSet;
/**
* 1.类的用途
* 2.@author1
* 3.@data2017/10/4 22:33
*/
public class CrashHandler implements Thread.UncaughtExceptionHandler {
/** Debug Log tag*/
public static final String TAG = "CrashHandler";
/** 是否开启日志输出,在Debug状态下开启,
* 在Release状态下关闭以提示程序性能
* */
public static final boolean DEBUG = true;
/** 系统默认的UncaughtException处理类 */
private Thread.UncaughtExceptionHandler mDefaultHandler;
/** CrashHandler实例 */
private static CrashHandler INSTANCE;
/** 程序的Context对象 */
private Context mContext;
/** 使用Properties来保存设备的信息和错误堆栈信息*/
private Properties mDeviceCrashInfo = new Properties();
private static final String VERSION_NAME = "versionName";
private static final String VERSION_CODE = "versionCode";
private static final String STACK_TRACE = "STACK_TRACE";
/** 错误报告文件的扩展名 */
private static final String CRASH_REPORTER_EXTENSION = ".cr";
private static Object syncRoot = new Object();
/** 保证只有一个CrashHandler实例 */
private CrashHandler() {}
/** 获取CrashHandler实例 ,单例模式*/
public static CrashHandler getInstance() {
/* if (INSTANCE == null) {
INSTANCE = new CrashHandler();
}
return INSTANCE;*/
// 防止多线程访问安全,这里使用了双重锁
if (INSTANCE == null)
{
synchronized (syncRoot)
{
if (INSTANCE == null)
{
INSTANCE = new CrashHandler();
}
}
}
return INSTANCE;
}
/**
* 初始化,注册Context对象,
* 获取系统默认的UncaughtException处理器,
* 设置该CrashHandler为程序的默认处理器
* @param ctx
*/
public void init(Context ctx) {
mContext = ctx;
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* 当UncaughtException发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
//如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
//Sleep一会后结束程序
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Log.i(TAG, "Error : ", e);
}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
/**
* 自定义错误处理,收集错误信息
* 发送错误报告等操作均在此完成.
* 开发者可以根据自己的情况来自定义异常处理逻辑
* @param ex
* @return true:如果处理了该异常信息;否则返回false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
Log.i(TAG, "handleException --- ex==null");
return true;
}
//获取得到错误信息
final String msg = ex.getLocalizedMessage();
if(msg == null) {
return false;
}
//使用Toast来显示异常信息
new Thread() {
@Override
public void run() {
Looper.prepare();
if(DEBUG){
Log.i(TAG, "异常信息->"+msg);
Toast toast = Toast.makeText(mContext, "程序出错,即将退出:\r\n" + msg,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
//保存错误报告文件
LogToFile.i("my",msg);//单独写的一个log写入类,下面已提供了该类**
}
// MsgPrompt.showMsg(mContext, "程序出错啦", msg+"\n点确认退出");
Looper.loop();
}
}.start();
//收集设备信息
collectCrashDeviceInfo(mContext);
//保存错误报告文件
saveCrashInfoToFile(ex);
//发送错误报告到服务器
//sendCrashReportsToServer(mContext);
return true;
}
/**
* 在程序启动时候, 可以调用该函数来发送以前没有发送的报告
*/
public void sendPreviousReportsToServer() {
sendCrashReportsToServer(mContext);
}
/**
* 把错误报告发送给服务器,包含新产生的和以前没发送的.
* @param ctx
*/
private void sendCrashReportsToServer(Context ctx) {
String[] crFiles = getCrashReportFiles(ctx);
if (crFiles != null && crFiles.length > 0) {
TreeSet<String> sortedFiles = new TreeSet<String>();
sortedFiles.addAll(Arrays.asList(crFiles));
for (String fileName : sortedFiles) {
File cr = new File(ctx.getFilesDir(), fileName);
postReport(cr);
cr.delete();// 删除已发送的报告
}
}
}
private void postReport(File file) {
// TODO 发送错误报告到服务器
}
/**
* 获取错误报告文件名
* @param ctx
* @return
*/
private String[] getCrashReportFiles(Context ctx) {
File filesDir = ctx.getFilesDir();
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(CRASH_REPORTER_EXTENSION);
}
};
return filesDir.list(filter);
}
/**
* 保存错误信息到文件中
* @param ex
* @return
*/
private String saveCrashInfoToFile(Throwable ex) {
Writer info = new StringWriter();
PrintWriter printWriter = new PrintWriter(info);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
String result = info.toString();
printWriter.close();
mDeviceCrashInfo.put("EXEPTION", ex.getLocalizedMessage());
mDeviceCrashInfo.put(STACK_TRACE, result);
try {
//long timestamp = System.currentTimeMillis();
Time t = new Time("GMT+8");
t.setToNow(); // 取得系统时间
int date = t.year * 10000 + t.month * 100 + t.monthDay;
int time = t.hour * 10000 + t.minute * 100 + t.second;
String fileName = "crash-" + date + "-" + time + CRASH_REPORTER_EXTENSION;
FileOutputStream trace = mContext.openFileOutput(fileName,
Context.MODE_PRIVATE);
mDeviceCrashInfo.store(trace, "");
trace.flush();
trace.close();
return fileName;
} catch (Exception e) {
Log.i(TAG, "an error occured while writing report file...", e);
}
return null;
}
/**
* 收集程序崩溃的设备信息
*
* @param ctx
*/
public void collectCrashDeviceInfo(Context ctx) {
try {
PackageManager pm = ctx.getPackageManager();
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(),
PackageManager.GET_ACTIVITIES);
if (pi != null) {
mDeviceCrashInfo.put(VERSION_NAME,
pi.versionName == null ? "not set" : pi.versionName);
mDeviceCrashInfo.put(VERSION_CODE, ""+pi.versionCode);
}
} catch (PackageManager.NameNotFoundException e) {
Log.i(TAG, "Error while collect package info", e);
}
//使用反射来收集设备信息.在Build类中包含各种设备信息,
//例如: 系统版本号,设备生产商 等帮助调试程序的有用信息
//具体信息请参考后面的截图
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
mDeviceCrashInfo.put(field.getName(), ""+field.get(null));
if (DEBUG) {
Log.i(TAG, field.getName() + " : " + field.get(null));
}
} catch (Exception e) {
Log.i(TAG, "Error while collect crash info", e);
}
}
}
}
在LogToFile的代码:
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* 1.类的用途
* 2.@author1
* 3.@data2017/10/4 22:36
*/
public class LogToFile {
//日志是否需要读写开关
public static final boolean DEBUG_FLAG = false;
private static String TAG = "LogToFile";
private static String logPath = null;//log日志存放路径
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);//日期格式;
private static Date date = new Date();//因为log日志是使用日期命名的,使用静态成员变量主要是为了在整个程序运行期间只存在一个.log文件中;
/**
* 初始化,须在使用之前设置,最好在Application创建时调用
*
* @param context
*/
public static void init(Context context) {
logPath = getFilePath(context) + "/Logs";//获得文件储存路径,在后面加"/Logs"建立子文件夹
}
/**
* 获得文件存储路径
*
* @return
*/
private static String getFilePath(Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable()) {//如果外部储存可用
return context.getExternalFilesDir(null).getPath();//获得外部存储路径,默认路径为 /storage/emulated/0/Android/data/com.waka.workspace.logtofile/files/Logs/log_2016-03-14_16-15-09.log
} else {
return context.getFilesDir().getPath();//直接存在/data/data里,非root手机是看不到的
}
}
private static final char VERBOSE = 'v';
private static final char DEBUG = 'd';
private static final char INFO = 'i';
private static final char WARN = 'w';
private static final char ERROR = 'e';
public static void v(String tag, String msg) {
if(DEBUG_FLAG){
writeToFile(VERBOSE, tag, msg);
}
}
public static void d(String tag, String msg) {
if(DEBUG_FLAG){
writeToFile(DEBUG, tag, msg);
}
}
public static void i(String tag, String msg) {
if(DEBUG_FLAG){
writeToFile(INFO, tag, msg);
}
}
public static void w(String tag, String msg) {
if(DEBUG_FLAG){
writeToFile(WARN, tag, msg);
}
}
public static void e(String tag, String msg) {
if(DEBUG_FLAG){
writeToFile(ERROR, tag, msg);
}
}
/**
* 将log信息写入文件中
*
* @param type
* @param tag
* @param msg
*/
private static void writeToFile(char type, String tag, String msg) {
if (null == logPath) {
Log.i(TAG, "logPath == null ,未初始化LogToFile");
return;
}
String fileName = logPath + "/log_" + dateFormat.format(new Date()) + ".log";//log日志名,使用时间命名,保证不重复
String log = dateFormat.format(date) + " " + type + " " + tag + " " + msg + "\n";//log日志内容,可以自行定制
//如果父路径不存在
File file = new File(logPath);
if (!file.exists()) {
file.mkdirs();//创建父路径
}
FileOutputStream fos = null;//FileOutputStream会自动调用底层的close()方法,不用关闭
BufferedWriter bw = null;
try {
fos = new FileOutputStream(fileName, true);//这里的第二个参数代表追加还是覆盖,true为追加,flase为覆盖
bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write(log);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();//关闭缓冲流
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最后在MainActivity中的代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int q = Integer.parseInt("q");
}
});
}
}
记得写dimens:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
新建一个values-w820dp中写dimens:
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
异常捕获的demo
最新推荐文章于 2019-10-23 16:08:18 发布