Android application捕获崩溃异常怎么办
Androidapplication捕获崩溃异常怎么办?
通用application
1、收集所有avtivity用于彻底退出应用
2、捕获崩溃异常,保存错误日志,并重启应用
publicclassHKBaseApplicationextendsApplication{ //activity对象列表,用于activity统一管理 privateListactivityList; //异常捕获 protectedbooleanisNeedCaughtExeption=true;//是否捕获未知异常 privatePendingIntentrestartIntent; privateMyUncaughtExceptionHandleruncaughtExceptionHandler; privateStringpackgeName; @Override publicvoidonCreate(){ super.onCreate(); activityList=newArrayList (); packgeName=getPackageName(); if(isNeedCaughtExeption){ cauchException(); } } //-------------------异常捕获-----捕获异常后重启系统-----------------// privatevoidcauchException(){ Intentintent=newIntent(); //参数1:包名,参数2:程序入口的activity intent.setClassName(packgeName,packgeName+".LoginActivity"); restartIntent=PendingIntent.getActivity(getApplicationContext(),-1,intent, Intent.FLAG_ACTIVITY_NEW_TASK); //程序崩溃时触发线程 uncaughtExceptionHandler=newMyUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler); } //创建服务用于捕获崩溃异常 privateclassMyUncaughtExceptionHandlerimplementsUncaughtExceptionHandler{ @Override publicvoiduncaughtException(Threadthread,Throwableex){ //保存错误日志 saveCatchInfo2File(ex); //1秒钟后重启应用 AlarmManagermgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC,System.currentTimeMillis()+1000,restartIntent); //关闭当前应用 finishAllActivity(); finishProgram(); } }; /** *保存错误信息到文件中 * *@return返回文件名称 */ privateStringsaveCatchInfo2File(Throwableex){ Writerwriter=newStringWriter(); PrintWriterprintWriter=newPrintWriter(writer); ex.printStackTrace(printWriter); Throwablecause=ex.getCause(); while(cause!=null){ cause.printStackTrace(printWriter); cause=cause.getCause(); } printWriter.close(); Stringsb=writer.toString(); try{ DateFormatformatter=newSimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); Stringtime=formatter.format(newDate()); StringfileName=time+".txt"; System.out.println("fileName:"+fileName); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ StringfilePath=Environment.getExternalStorageDirectory()+"/HKDownload/"+packgeName +"/crash/"; Filedir=newFile(filePath); if(!dir.exists()){ if(!dir.mkdirs()){ //创建目录失败:一般是因为SD卡被拔出了 return""; } } System.out.println("filePath+fileName:"+filePath+fileName); FileOutputStreamfos=newFileOutputStream(filePath+fileName); fos.write(sb.getBytes()); fos.close(); //文件保存完了之后,在应用下次启动的时候去检查错误日志,发现新的错误日志,就发送给开发者 } returnfileName; }catch(Exceptione){ System.out.println("anerroroccuredwhilewritingfile..."+e.getMessage()); } returnnull; } //------------------------------activity管理-----------------------// //activity管理:从列表中移除activity publicvoidremoveActivity(Activityactivity){ activityList.remove(activity); } //activity管理:添加activity到列表 publicvoidaddActivity(Activityactivity){ activityList.add(activity); } //activity管理:结束所有activity publicvoidfinishAllActivity(){ for(Activityactivity:activityList){ if(null!=activity){ activity.finish(); } } } //结束线程,一般与finishAllActivity()一起使用 //例如:finishAllActivity;finishProgram(); publicvoidfinishProgram(){ android.os.Process.killProcess(android.os.Process.myPid()); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。