欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android application捕获崩溃异常怎么办

程序员文章站 2023-12-05 23:38:04
android application捕获崩溃异常怎么办? 通用 application 1、收集所有 avtivity 用于彻底退出应用 2、捕获崩溃异常,保存错误...

android application捕获崩溃异常怎么办?

通用 application
1、收集所有 avtivity 用于彻底退出应用
2、捕获崩溃异常,保存错误日志,并重启应用

public class hkbaseapplication extends application {
 // activity对象列表,用于activity统一管理
 private list<activity> activitylist;
 // 异常捕获
 protected boolean isneedcaughtexeption = true;// 是否捕获未知异常
 private pendingintent restartintent;
 private myuncaughtexceptionhandler uncaughtexceptionhandler;
 private string packgename;

 @override
 public void oncreate() {
 super.oncreate();

 activitylist = new arraylist<activity>();
 packgename = getpackagename();

 if (isneedcaughtexeption) {
  cauchexception();
 }
 }

 // -------------------异常捕获-----捕获异常后重启系统-----------------//

 private void cauchexception() {
 intent intent = new intent();
 // 参数1:包名,参数2:程序入口的activity
 intent.setclassname(packgename, packgename + ".loginactivity");
 restartintent = pendingintent.getactivity(getapplicationcontext(), -1, intent,
  intent.flag_activity_new_task);

 // 程序崩溃时触发线程
 uncaughtexceptionhandler = new myuncaughtexceptionhandler();
 thread.setdefaultuncaughtexceptionhandler(uncaughtexceptionhandler);
 }

 // 创建服务用于捕获崩溃异常
 private class myuncaughtexceptionhandler implements uncaughtexceptionhandler {
 @override
 public void uncaughtexception(thread thread, throwable ex) {
  // 保存错误日志
  savecatchinfo2file(ex);

  // 1秒钟后重启应用
  alarmmanager mgr = (alarmmanager) getsystemservice(context.alarm_service);
  mgr.set(alarmmanager.rtc, system.currenttimemillis() + 1000, restartintent);

  // 关闭当前应用
  finishallactivity();
  finishprogram();
 }
 };

 /**
 * 保存错误信息到文件中
 * 
 * @return 返回文件名称
 */
 private string savecatchinfo2file(throwable ex) {
 writer writer = new stringwriter();
 printwriter printwriter = new printwriter(writer);
 ex.printstacktrace(printwriter);
 throwable cause = ex.getcause();
 while (cause != null) {
  cause.printstacktrace(printwriter);
  cause = cause.getcause();
 }
 printwriter.close();
 string sb = writer.tostring();
 try {
  dateformat formatter = new simpledateformat("yyyy-mm-dd-hh-mm-ss");
  string time = formatter.format(new date());
  string filename = time + ".txt";
  system.out.println("filename:" + filename);
  if (environment.getexternalstoragestate().equals(environment.media_mounted)) {
  string filepath = environment.getexternalstoragedirectory() + "/hkdownload/" + packgename
   + "/crash/";
  file dir = new file(filepath);
  if (!dir.exists()) {
   if (!dir.mkdirs()) {
   // 创建目录失败: 一般是因为sd卡被拔出了
   return "";
   }
  }
  system.out.println("filepath + filename:" + filepath + filename);
  fileoutputstream fos = new fileoutputstream(filepath + filename);
  fos.write(sb.getbytes());
  fos.close();
  //文件保存完了之后,在应用下次启动的时候去检查错误日志,发现新的错误日志,就发送给开发者
  }
  return filename;
 } catch (exception e) {
  system.out.println("an error occured while writing file..." + e.getmessage());
 }
 return null;
 }

 // ------------------------------activity管理-----------------------//

 // activity管理:从列表中移除activity
 public void removeactivity(activity activity) {
 activitylist.remove(activity);
 }

 // activity管理:添加activity到列表
 public void addactivity(activity activity) {
 activitylist.add(activity);
 }

 // activity管理:结束所有activity
 public void finishallactivity() {
 for (activity activity : activitylist) {
  if (null != activity) {
  activity.finish();
  }
 }
 }

 // 结束线程,一般与finishallactivity()一起使用
 // 例如: finishallactivity;finishprogram();
 public void finishprogram() {
 android.os.process.killprocess(android.os.process.mypid());
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。