From 8b4ce0eea739ca69b8f1bf77452c40efa69a9035 Mon Sep 17 00:00:00 2001 From: endian11 <Dana_Lee1016@126.com> Date: 星期日, 29 九月 2019 17:08:30 +0800 Subject: [PATCH] 修改MyLog,删除2天以前的日志;崩溃是Toast提示 --- lib/src/main/java/com/anyun/exam/lib/crash/CrashHandler.java | 11 +++++ lib/src/main/java/com/anyun/exam/lib/MyLog.java | 97 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 88 insertions(+), 20 deletions(-) diff --git a/lib/src/main/java/com/anyun/exam/lib/MyLog.java b/lib/src/main/java/com/anyun/exam/lib/MyLog.java index 372772b..bec1066 100644 --- a/lib/src/main/java/com/anyun/exam/lib/MyLog.java +++ b/lib/src/main/java/com/anyun/exam/lib/MyLog.java @@ -1,5 +1,10 @@ package com.anyun.exam.lib; + + + + + /** * anyunProject(20190906) * Created by lzw on 2019/9/6. 17:23:17 @@ -9,22 +14,25 @@ import android.content.Context; -import android.os.Environment; -import android.text.TextUtils; -import android.util.Log; + import android.os.Environment; + import android.text.TextUtils; + import android.util.Log; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; + import java.io.BufferedWriter; + import java.io.File; + import java.io.FileWriter; + import java.io.IOException; + import java.text.SimpleDateFormat; + import java.util.Calendar; + import java.util.Date; + import java.util.regex.Matcher; + import java.util.regex.Pattern; public class MyLog { + private static final String TAG = "AYJiaKao"; - private static final String TAG = "AYJiaKao"; + private static Boolean MYLOG_SWITCH = true; // 鏃ュ織鏂囦欢鎬诲紑鍏� private static Boolean MYLOG_WRITE_TO_FILE = true;// 鏃ュ織鍐欏叆鏂囦欢寮�鍏� @@ -36,7 +44,7 @@ // 鏃ュ織鏂囦欢鍦╯dcard涓殑璺緞 private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/JiaKaolog"; - private static int SDCARD_LOG_FILE_SAVE_DAYS = 2;// sd鍗′腑鏃ュ織鏂囦欢鐨勬渶澶氫繚瀛樺ぉ鏁� + private static int SDCARD_LOG_FILE_SAVE_DAYS = 3;// sd鍗′腑鏃ュ織鏂囦欢鐨勬渶澶氫繚瀛樺ぉ鏁� private static String MYLOGFILEName = "Log.txt";// 鏈被杈撳嚭鐨勬棩蹇楁枃浠跺悕绉� @@ -180,9 +188,6 @@ public static void createIfNotExist() { - delFile(); - - String needWriteFiel = logfile.format(System.currentTimeMillis()); @@ -226,11 +231,7 @@ private static void writeLogtoFile(String mylogtype, final String tag, final String text) {// 鏂板缓鎴栨墦寮�鏃ュ織鏂囦欢 - - - String needWriteMessage = myLogSdf.format(System.currentTimeMillis()) + " " + tag + " " + text; - try { /**鍚庨潰杩欎釜鍙傛暟浠h〃鏄笉鏄鎺ヤ笂鏂囦欢涓師鏉ョ殑鏁版嵁锛屼笉杩涜瑕嗙洊**/ @@ -267,7 +268,7 @@ String needDelFiel = logfile.format(getDateBefore()); - String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JiaKaolog"; + String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/nvlog"; File file = new File(dirPath, needDelFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR @@ -277,6 +278,50 @@ } + } + + public static void delSubDirLogs() { + String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/nvlog"; + String regx = "^[0-9]{4}-"; + Pattern pattern = Pattern.compile(regx); + File file = new File(dirPath); + boolean needDel = false; + if (file.isDirectory()) { + Log.i(TAG, "subDirLogs: is a directory"); + File[] subFiles = file.listFiles(); + for (File f : + subFiles) { + String fName = f.getName(); + Log.i(TAG, "subDirLogs: 鏂囦欢鍚嶇О锛�" + fName); + if (!TextUtils.isEmpty(fName)) { + Matcher matcher = pattern.matcher(fName); + if (matcher.find()) { + + Log.i(TAG, "subDirLogs: 绗﹀悎瑙勫垯^[0-9]{4}- : " + matcher.group(0)); + for (int i = 0; i < SDCARD_LOG_FILE_SAVE_DAYS; i++) { + String needSaveFileName = logfile.format(getDateBefore(i)); + if (fName.contains(needSaveFileName)) { + Log.i(TAG, "delSubDirLogs: 姝og闇�瑕佷繚鐣欙細" + fName + " 鏃ユ湡锛�" + needSaveFileName); + needDel = false; + break; + } else { + needDel = true; + } + } + if (needDel) { + Log.i(TAG, "delSubDirLogs: 鍒犻櫎璇ユ枃浠讹細" + fName); + f.delete(); + } + + + } else { + Log.i(TAG, "delSubDirLogs: 涓嶇鍚堝垹闄よ鍒欙紝鍒欎笉鍒犻櫎锛�" + fName); + } + } + } + } else { + Log.i(TAG, "subDirLogs: is not a directory"); + } } @@ -298,4 +343,16 @@ } + private static Date getDateBefore(int i) { + Date nowtime = new Date(); + + Calendar now = Calendar.getInstance(); + + now.setTime(nowtime); + + now.set(Calendar.DATE, now.get(Calendar.DATE) - i); + + return now.getTime(); + } + } \ No newline at end of file diff --git a/lib/src/main/java/com/anyun/exam/lib/crash/CrashHandler.java b/lib/src/main/java/com/anyun/exam/lib/crash/CrashHandler.java index 6955b7a..26bc539 100644 --- a/lib/src/main/java/com/anyun/exam/lib/crash/CrashHandler.java +++ b/lib/src/main/java/com/anyun/exam/lib/crash/CrashHandler.java @@ -4,6 +4,8 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Build; +import android.os.Looper; +import android.widget.Toast; import com.anyun.exam.lib.MyLog; @@ -70,6 +72,15 @@ if (ex == null) { return false; } + //浣跨敤Toast鏄剧ず寮傚父淇℃伅 + new Thread(){ + @Override + public void run() { + Looper.prepare(); + Toast.makeText(mContext,"寰堟姳姝夛紝绋嬪簭鍑虹幇寮傚父锛屽嵆灏嗛��鍑猴紒",Toast.LENGTH_SHORT).show(); + Looper.loop(); + } + }.start(); //鏀堕泦璁惧淇℃伅 collectDeviceInfo(); saveCrashInfo2File(ex); -- Gitblit v1.8.0