android基础知识---重写系统Crash处理类保存上传和完美退出程序的方法
Posted Vincent__Lee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android基础知识---重写系统Crash处理类保存上传和完美退出程序的方法相关的知识,希望对你有一定的参考价值。
当今市场上android的手机型号和版本太多要做到完全适配几乎是完全不可能的,那么怎么才能获取其他的玩家的出错的信息呢!这里我们就要重新定义系统的Crash处理类了。
首先我们我们新建一个CustomCrashHandler类 实现UncaughtExceptionHandler接口,重写回调方法void uncaughtException(Thread thread, Throwable ex)
package com.example.admin.crashchuli;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
/**
* 自定义系统的Crash捕捉类,用Toast替换系统的对话框
* 将软件版本信息,设备信息,出错信息保存在sd卡中,方便上传到服务器中
* @author ls
*
*/
public class CustomCrashHandler implements Thread.UncaughtExceptionHandler {
private static final String TAG = "Activity";
private Context mContext;
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 (PackageManager.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(SDCARD_ROOT + File.separator + "crash" + File.separator);
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
*/
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;
}
}
当然不忘记加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
然后是就是在重写的Application类中的onCreate()方法中为它设置Crash处理类。
/**
* Created by ls on 2017/4/30.
*/
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
CustomCrashHandler mCustomCrashHandler = CustomCrashHandler.getInstance();
mCustomCrashHandler.setCustomCrashHanler(getApplicationContext());
}
}
记得在清单文件中设置application这个就不用说了吧
然后随便写个空指针异常
public class MainActivity extends BaseActivity {
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView.setText("crash");
}
}
然后我们就可看到在我们制定的文件夹下面有我们要的log的txt文件,剩下我就不说了就是简单的上传到服务器、
对了说下ExitAppUtils就是为了设置完美退出因为异常的时候会闪几次或者有点手机直接退出不了这肯定不行,所以就用ExitAppUtils来控制,当然不能所有activity都写所以要写在BaseActivity然后其他的继承就好了
package com.example.admin.crashchuli;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by admin on 2017/4/30.
*/
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExitAppUtils.getInstance().addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ExitAppUtils.getInstance().delActivity(this);
}
}
以上是关于android基础知识---重写系统Crash处理类保存上传和完美退出程序的方法的主要内容,如果未能解决你的问题,请参考以下文章