| | |
| | | import android.os.Environment; |
| | | import android.os.Looper; |
| | | |
| | | import com.doumee.keyCabinet.MApplication; |
| | | import com.doumee.keyCabinet.ui.guide.GuideActivity; |
| | | import com.doumee.keyCabinet.ui.main.MainActivity; |
| | | import com.doumee.keyCabinet.utils.update.FileUtil; |
| | | import com.doumee.lib_coremodel.util.SpUtil; |
| | | import com.mc.enjoysdk.McSystemUi; |
| | | |
| | | import java.io.BufferedWriter; |
| | | import java.io.ByteArrayOutputStream; |
| | |
| | | import java.lang.Thread.UncaughtExceptionHandler; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class CrashHandler implements UncaughtExceptionHandler { |
| | | private static final String TAG = "CrashHandler"; |
| | | private static String PATH = Environment.getExternalStorageDirectory().getPath() + "/log/"; |
| | | private static String PATH = "/log/"; |
| | | public static final String FILE_NAME = "crash"; |
| | | //log文件的后缀名 |
| | | private static final String FILE_NAME_SUFFIX = ".txt"; |
| | |
| | | public void uncaughtException(Thread thread, Throwable ex) { |
| | | try { |
| | | //导出异常信息到SD卡中 |
| | | dumpExceptionToSDCard(ex); |
| | | //System.out.println("重启应用"); |
| | | // 创建一个新的启动意图 |
| | | /*Intent intent = new Intent(mContext, MainActivity.class); |
| | | // 设置FLAG_ACTIVITY_CLEAR_TASK标志位 |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| | | // 使用PendingIntent包装启动意图 |
| | | PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT); |
| | | // 获取AlarmManager实例 |
| | | AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| | | // 在500毫秒后触发重启操作 |
| | | alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 10, pendingIntent);*/ |
| | | McSystemUi mcSystemUi = McSystemUi.getInstance(mContext); |
| | | MCUtils.show(mcSystemUi); |
| | | long time = SpUtil.getLong("err_app"); |
| | | SpUtil.saveLong("err_app",System.currentTimeMillis()); |
| | | if(System.currentTimeMillis()-time>60000){ |
| | | //报错间隔60秒,才重启 |
| | | //System.out.println("重启应用"); |
| | | // 创建一个新的启动意图 |
| | | Intent intent = new Intent(mContext, MainActivity.class); |
| | | // 设置FLAG_ACTIVITY_CLEAR_TASK标志位 |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| | | // 使用PendingIntent包装启动意图 |
| | | PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT); |
| | | // 获取AlarmManager实例 |
| | | AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| | | // 在500毫秒后触发重启操作 |
| | | alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 10, pendingIntent); |
| | | |
| | | android.os.Process.killProcess(android.os.Process.myPid()); |
| | | System.exit(1); |
| | | android.os.Process.killProcess(android.os.Process.myPid()); |
| | | System.exit(1); |
| | | } |
| | | //这里可以通过网络上传异常信息到服务器,便于开发人员分析日志从而解决bug |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("错误日志1"+e.getMessage()); |
| | | } |
| | | //打印出当前调用栈信息 |
| | | ex.printStackTrace(); |
| | | //如果系统提供了默认的异常处理器,则交给系统去结束我们的程序,否则就由我们自己结束自己 |
| | | if (mDefaultHandler != null) { |
| | | mDefaultHandler.uncaughtException(thread, ex); |
| | | } else { |
| | | android.os.Process.killProcess(android.os.Process.myPid()); |
| | | } |
| | | |
| | | if (!handleException(ex) && mDefaultHandler != null) { |
| | | // 如果用户没有处理则让系统默认的异常处理器来处理 |
| | | mDefaultHandler.uncaughtException(thread, ex); |
| | | } else { |
| | | /*try { |
| | | Thread.sleep(500); |
| | | } catch (InterruptedException e) { |
| | | Log.e(TAG, "error : ", e); |
| | | }*/ |
| | | /*System.out.println("重启应用"); |
| | | // 创建一个新的启动意图 |
| | | Intent intent = new Intent(mContext, GuideActivity.class); |
| | | // 设置FLAG_ACTIVITY_CLEAR_TASK标志位 |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| | | // 使用PendingIntent包装启动意图 |
| | | PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT); |
| | | // 获取AlarmManager实例 |
| | | AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| | | // 在500毫秒后触发重启操作 |
| | | alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 10, pendingIntent); |
| | | |
| | | android.os.Process.killProcess(android.os.Process.myPid()); |
| | | System.exit(1);*/ |
| | | /*// 未捕获异常写文件 |
| | | writeCrashInfoToFile(ex); |
| | | // 退出整个应用 |
| | | //打印出当前调用栈信息 |
| | | ex.printStackTrace(); |
| | | //如果系统提供了默认的异常处理器,则交给系统去结束我们的程序,否则就由我们自己结束自己 |
| | | if (mDefaultHandler != null) { |
| | | mDefaultHandler.uncaughtException(thread, ex); |
| | | } else { |
| | | System.exit(0); |
| | | }*/ |
| | | android.os.Process.killProcess(android.os.Process.myPid()); |
| | | } |
| | | |
| | | if (!handleException(ex) && mDefaultHandler != null) { |
| | | // 如果用户没有处理则让系统默认的异常处理器来处理 |
| | | mDefaultHandler.uncaughtException(thread, ex); |
| | | } |
| | | long current = System.currentTimeMillis(); |
| | | String time2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current)); |
| | | try { |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | | PrintStream print = new PrintStream(out); |
| | | //导出发生异常的时间 |
| | | print.println(time2); |
| | | //导出手机信息 |
| | | dumpPhoneInfo(print); |
| | | print.println(); |
| | | ex.printStackTrace(print); |
| | | SpUtil.saveString(FILE_NAME+time2,new String(out.toByteArray())); |
| | | |
| | | print.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | System.out.println("错误日志2"+e.getMessage()); |
| | | //LogUtils.e(TAG, "dump crash info failed"); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | |
| | | } catch (NameNotFoundException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("错误日志3"+e.getMessage()); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | |
| | | Build.BRAND, |
| | | // android SDK版本 |
| | | Build.VERSION.RELEASE); |
| | | //sp.setString(Constants.SP_CRASH_EXCEPTION, errorLog); |
| | | return null; |
| | | } |
| | | |
| | |
| | | print.println(); |
| | | ex.printStackTrace(print); |
| | | SpUtil.saveString(FILE_NAME+time,new String(out.toByteArray())); |
| | | |
| | | print.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | //LogUtils.e(TAG, "dump crash info failed"); |
| | | } |
| | | //以当前时间创建log文件 |
| | | File file = new File(PATH , FILE_NAME + time + FILE_NAME_SUFFIX); |
| | | File file = new File(FileUtil.getFile(mContext)+PATH , FILE_NAME + time + FILE_NAME_SUFFIX); |
| | | try { |
| | | PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); |
| | | //导出发生异常的时间 |