青春须早为,岂能长少年。这篇文章主要讲述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 = https://www.songbingjia.com/android/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 MyApplicationextends 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文件,剩下我就不说了就是简单的上传到服务器、
【android基础知识---重写系统Crash处理类保存上传和完美退出程序的方法】对了说下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实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)
- 彻底明确Android中AIDL及其使用
- 边做项目边学Android小白会遇到的问题--Appcompat_V7问题
- Android修改内核
- jQuery学习笔记之DOM树创建新节点append方法
- BeanFactory和ApplicationContext的介绍
- 怎样高速启动Android模拟器(Android Emulator)
- H5+App的认识
- 在Android上Kotlin的单元测试(KAD22)