使用CrashHandler来获取应用的crash信息

 2023-09-05 阅读 115 评论 0

摘要:源码地址https://github.com/king1039/android-art-res/tree/master/Chapter_13/CrashTest/src/com/ryg/crashtest public class CrashHandler implements UncaughtExceptionHandler {private static final String TAG = "CrashHandler";private static final boo

源码地址https://github.com/king1039/android-art-res/tree/master/Chapter_13/CrashTest/src/com/ryg/crashtest

public class CrashHandler implements UncaughtExceptionHandler {private static final String TAG = "CrashHandler";private static final boolean DEBUG = true;private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/CrashTest/log/";private static final String FILE_NAME = "crash";private static final String FILE_NAME_SUFFIX = ".trace";private static CrashHandler sInstance = new CrashHandler();private UncaughtExceptionHandler mDefaultCrashHandler;private Context mContext;private CrashHandler() {}public static CrashHandler getInstance() {return sInstance;}public void init(Context context) {mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();Thread.setDefaultUncaughtExceptionHandler(this);mContext = context.getApplicationContext();}/*** 这个是最关键的函数,当程序中有未被捕获的异常,系统将会自动调用#uncaughtException方法* thread为出现未捕获异常的线程,ex为未捕获的异常,有了这个ex,我们就可以得到异常信息。*/@Overridepublic void uncaughtException(Thread thread, Throwable ex) {try {//导出异常信息到SD卡中
            dumpExceptionToSDCard(ex);uploadExceptionToServer();//这里可以通过网络上传异常信息到服务器,便于开发人员分析日志从而解决bug} catch (IOException e) {e.printStackTrace();}ex.printStackTrace();//如果系统提供了默认的异常处理器,则交给系统去结束我们的程序,否则就由我们自己结束自己if (mDefaultCrashHandler != null) {mDefaultCrashHandler.uncaughtException(thread, ex);} else {Process.killProcess(Process.myPid());}}private void dumpExceptionToSDCard(Throwable ex) throws IOException {//如果SD卡不存在或无法使用,则无法把异常信息写入SD卡if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {if (DEBUG) {Log.w(TAG, "sdcard unmounted,skip dump exception");return;}}File dir = new File(PATH);if (!dir.exists()) {dir.mkdirs();}long current = System.currentTimeMillis();String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current));File file = new File(PATH + FILE_NAME + time + FILE_NAME_SUFFIX);try {PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));pw.println(time);dumpPhoneInfo(pw);pw.println();ex.printStackTrace(pw);pw.close();} catch (Exception e) {Log.e(TAG, "dump crash info failed");}}private void dumpPhoneInfo(PrintWriter pw) throws NameNotFoundException {PackageManager pm = mContext.getPackageManager();PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);pw.print("App Version: ");pw.print(pi.versionName);pw.print('_');pw.println(pi.versionCode);//android版本号pw.print("OS Version: ");pw.print(Build.VERSION.RELEASE);pw.print("_");pw.println(Build.VERSION.SDK_INT);//手机制造商pw.print("Vendor: ");pw.println(Build.MANUFACTURER);//手机型号pw.print("Model: ");pw.println(Build.MODEL);//cpu架构pw.print("CPU ABI: ");pw.println(Build.CPU_ABI);}private void uploadExceptionToServer() {//TODO Upload Exception Message To Your Web Server
    }}

使用

public class TestApp extends Application {private static TestApp sInstance;@Overridepublic void onCreate() {super.onCreate();sInstance = this;//在这里为应用设置异常处理程序,然后我们的程序才能捕获未处理的异常CrashHandler crashHandler = CrashHandler.getInstance();crashHandler.init(this);}public static TestApp getInstance() {return sInstance;}}

测试

public class CrashActivity extends Activity implements OnClickListener {private Button mButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_crash);initView();}private void initView() {mButton = (Button) findViewById(R.id.button1);mButton.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v == mButton) {// 在这里模拟异常抛出情况,人为抛出一个运行时异常throw new RuntimeException("自定义异常:这是自己抛出的异常");}}}

 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/45.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息