endian11
2019-09-29 8b4ce0eea739ca69b8f1bf77452c40efa69a9035
修改MyLog,删除2天以前的日志;崩溃是Toast提示
2个文件已修改
88 ■■■■ 已修改文件
lib/src/main/java/com/anyun/exam/lib/MyLog.java 77 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/crash/CrashHandler.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
@@ -20,11 +25,14 @@
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 Boolean MYLOG_SWITCH = true; // 日志文件总开关
    private static Boolean MYLOG_WRITE_TO_FILE = true;// 日志写入文件开关
@@ -36,7 +44,7 @@
    // 日志文件在sdcard中的路径
    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 {
/**后面这个参数代表是不是要接上文件中原来的数据,不进行覆盖**/
@@ -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: 此log需要保留:" + 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();
    }
}
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);