-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liyiwei
committed
Aug 29, 2018
1 parent
9e64b08
commit a052663
Showing
5 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
app/src/main/java/group/tonight/vipvideohelper/other/CrashHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package group.tonight.vipvideohelper.other; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.os.Environment; | ||
import android.util.Log; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
public class CrashHandler implements Thread.UncaughtExceptionHandler { | ||
private static final String TAG = "CrashHandler"; | ||
private static final boolean DEBUG = true; | ||
//文件路径 | ||
private static final String PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "crash"; | ||
private static final String FILE_NAME = "crash"; | ||
private static final String FILE_NAME_SUFEIX = ".txt"; | ||
private static Thread.UncaughtExceptionHandler mDefaultCrashHandler; | ||
private static CrashHandler mCrashHandler = new CrashHandler(); | ||
private Context mContext; | ||
|
||
private long MAX_CRASH_FILE_SIZE = 200 * 1024;//最大日志量200KB | ||
// private String mAppHashKey; | ||
|
||
private CrashHandler() { | ||
} | ||
|
||
public static CrashHandler getInstance() { | ||
return mCrashHandler; | ||
} | ||
|
||
public void init(Context context) { | ||
mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); | ||
Thread.setDefaultUncaughtExceptionHandler(this); | ||
mContext = context.getApplicationContext(); | ||
// mAppHashKey = UMUtils.getAppHashKey(mContext); | ||
} | ||
|
||
@Override | ||
public void uncaughtException(Thread thread, Throwable ex) { | ||
try { | ||
//将文件写入sd卡 | ||
// if (BuildConfig.DEBUG) { | ||
// KLog.e(ex); | ||
writeToSDcard(ex); | ||
// } | ||
//写入后在这里可以进行上传操作 | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} catch (PackageManager.NameNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
ex.printStackTrace(); | ||
//如果系统提供了默认异常处理就交给系统进行处理,否则自己进行处理。 | ||
if (mDefaultCrashHandler != null) { | ||
mDefaultCrashHandler.uncaughtException(thread, ex); | ||
} else { | ||
android.os.Process.killProcess(android.os.Process.myPid()); | ||
} | ||
} | ||
|
||
//将异常写入文件 | ||
private void writeToSDcard(Throwable ex) throws IOException, PackageManager.NameNotFoundException { | ||
//如果没有SD卡,直接返回 | ||
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { | ||
return; | ||
} | ||
File filedir = new File(PATH); | ||
if (!filedir.exists()) { | ||
boolean mkdirs = filedir.mkdirs(); | ||
} | ||
long currenttime = System.currentTimeMillis(); | ||
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(currenttime)); | ||
|
||
File exfile = new File(PATH + File.separator + FILE_NAME + time + FILE_NAME_SUFEIX); | ||
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(exfile))); | ||
Log.e("错误日志文件路径", "" + exfile.getAbsolutePath()); | ||
pw.println(time); | ||
PackageManager pm = mContext.getPackageManager(); | ||
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); | ||
// pw.println("Host:" + RequestUtils.DEFAULT_HOST); | ||
// pw.println("AppHashKey:" + mAppHashKey); | ||
//当前版本号 | ||
pw.println("App Version:" + pi.versionName + "_" + pi.versionCode); | ||
//当前系统 | ||
pw.println("OS version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT); | ||
//制造商 | ||
pw.println("Vendor:" + Build.MANUFACTURER); | ||
//手机型号 | ||
pw.println("Model:" + Build.MODEL); | ||
//CPU架构 | ||
pw.println("CPU ABI:" + Build.CPU_ABI); | ||
|
||
ex.printStackTrace(pw); | ||
pw.close(); | ||
|
||
long folderSize = getFolderSize(filedir); | ||
String fileSize = convertFileSize(folderSize); | ||
// KLog.e("crash文件夹大小:" + fileSize); | ||
while (folderSize > MAX_CRASH_FILE_SIZE) { | ||
deleteOldestFile(filedir); | ||
} | ||
} | ||
|
||
/** | ||
* 获取文件夹大小 | ||
* | ||
* @param file File实例 | ||
* @return long | ||
*/ | ||
public static long getFolderSize(File file) { | ||
long size = 0; | ||
try { | ||
File[] fileList = file.listFiles(); | ||
for (File aFileList : fileList) { | ||
if (aFileList.isDirectory()) { | ||
size = size + getFolderSize(aFileList); | ||
} else { | ||
size = size + aFileList.length(); | ||
|
||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
//return size/1048576; | ||
return size; | ||
} | ||
|
||
/** | ||
* 删除指定文件夹中最旧的文件 | ||
* | ||
* @param dir | ||
*/ | ||
public static void deleteOldestFile(File dir) { | ||
if (dir == null) { | ||
return; | ||
} | ||
File[] files = dir.listFiles(); | ||
if (files == null) { | ||
return; | ||
} | ||
List<File> fileList = Arrays.asList(files); | ||
if (!fileList.isEmpty()) { | ||
File target = fileList.get(0); | ||
String name = target.getName(); | ||
boolean delete = target.delete(); | ||
// KLog.e("是否删除成功---》" + delete + " " + name); | ||
for (File file : fileList) { | ||
// KLog.e(file.getName() + " " + file.lastModified()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 将文件长度转换为带单位的显示形式 | ||
* | ||
* @param size new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"abc.txt").length() | ||
* @return | ||
*/ | ||
public static String convertFileSize(long size) { | ||
long kb = 1024; | ||
long mb = kb * 1024; | ||
long gb = mb * 1024; | ||
//%.2f 即是保留两位小数的浮点数,后面跟上对应单位就可以了,不得不说java很方便 | ||
if (size >= gb) { | ||
return String.format("%.2f GB", (float) size / gb); | ||
} else if (size >= mb) { | ||
float f = (float) size / mb; | ||
//如果大于100MB就不用保留小数位啦 | ||
return String.format(f > 100 ? "%.0f MB" : "%.2f MB", f); | ||
} else if (size >= kb) { | ||
float f = (float) size / kb; | ||
//如果大于100kB就不用保留小数位了 | ||
return String.format(f > 100 ? "%.0f KB" : "%.2f KB", f); | ||
} else | ||
return String.format("%d B", size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters