Compare commits
6 Commits
appbase-v1
...
appbase-v1
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d694992b7 | |||
| 6f5aa807c0 | |||
| f897f6e9ab | |||
| e2c73fdec0 | |||
| 4fcc5f9689 | |||
| 4208cda32f |
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Mon May 11 10:04:02 HKT 2026
|
#Mon May 11 14:45:14 HKT 2026
|
||||||
stageCount=3
|
stageCount=5
|
||||||
libraryProject=libappbase
|
libraryProject=libappbase
|
||||||
baseVersion=15.20
|
baseVersion=15.20
|
||||||
publishVersion=15.20.2
|
publishVersion=15.20.4
|
||||||
buildCount=0
|
buildCount=0
|
||||||
baseBetaVersion=15.20.3
|
baseBetaVersion=15.20.5
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package cc.winboll.studio.appbase;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import cc.winboll.studio.libappbase.ToastUtils;
|
||||||
|
|
||||||
|
public class CrashTestActivity extends Activity {
|
||||||
|
|
||||||
|
public static final String TAG = "CrashTestActivity";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_crash_test);
|
||||||
|
LogUtils.d(TAG, "CrashTestActivity onCreate()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onBack(View view) {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onTestCrash(View view) {
|
||||||
|
LogUtils.d(TAG, "onTestCrash()");
|
||||||
|
ToastUtils.show("测试布局崩溃...");
|
||||||
|
}
|
||||||
|
}
|
||||||
60
appbase/src/main/res/layout/activity_crash_test.xml
Normal file
60
appbase/src/main/res/layout/activity_crash_test.xml
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:padding="0dp"
|
||||||
|
android:background="?attr/activityBackgroundColor">
|
||||||
|
|
||||||
|
<android.widget.Toolbar
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="?attr/toolbarBackgroundColor"
|
||||||
|
android:id="@+id/toolbar"/>
|
||||||
|
|
||||||
|
<ScrollView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1.0">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:gravity="center_vertical">
|
||||||
|
|
||||||
|
<cc.winboll.studio.appbase.UndefinedCustomView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_margin="10dp"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="返回"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textColor="?attr/activityTextColor"
|
||||||
|
android:background="?attr/buttonBackgroundColor"
|
||||||
|
android:paddingVertical="12dp"
|
||||||
|
android:layout_marginHorizontal="24dp"
|
||||||
|
android:onClick="onBack"
|
||||||
|
android:layout_margin="10dp"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="测试崩溃"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textColor="?attr/activityTextColor"
|
||||||
|
android:background="?attr/buttonBackgroundColor"
|
||||||
|
android:paddingVertical="12dp"
|
||||||
|
android:layout_marginHorizontal="24dp"
|
||||||
|
android:onClick="onTestCrash"
|
||||||
|
android:layout_margin="10dp"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</ScrollView>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Mon May 11 10:04:02 HKT 2026
|
#Mon May 11 14:45:14 HKT 2026
|
||||||
stageCount=3
|
stageCount=5
|
||||||
libraryProject=libappbase
|
libraryProject=libappbase
|
||||||
baseVersion=15.20
|
baseVersion=15.20
|
||||||
publishVersion=15.20.2
|
publishVersion=15.20.4
|
||||||
buildCount=0
|
buildCount=0
|
||||||
baseBetaVersion=15.20.3
|
baseBetaVersion=15.20.5
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import android.content.Intent;
|
|||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
@@ -165,6 +164,7 @@ public final class CrashHandler {
|
|||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
LogUtils.d(TAG, "gotoCrashActiviy: ");
|
LogUtils.d(TAG, "gotoCrashActiviy: ");
|
||||||
|
|
||||||
|
|
||||||
// 根据保险丝状态选择启动的崩溃页面
|
// 根据保险丝状态选择启动的崩溃页面
|
||||||
if (AppCrashSafetyWire.getInstance().isAppCrashSafetyWireOK()) {
|
if (AppCrashSafetyWire.getInstance().isAppCrashSafetyWireOK()) {
|
||||||
LogUtils.d(TAG, "gotoCrashActiviy: isAppCrashSafetyWireOK");
|
LogUtils.d(TAG, "gotoCrashActiviy: isAppCrashSafetyWireOK");
|
||||||
@@ -186,13 +186,14 @@ public final class CrashHandler {
|
|||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (GlobalApplication.isDebugging()&&AppCrashSafetyWire.getInstance().isAppCrashSafetyWireOK()) {
|
if (GlobalApplication.isDebugging()) {
|
||||||
// 如果是 debug 版,启动崩溃页面窗口
|
// 如果是 debug 版,启动崩溃页面窗口
|
||||||
app.startActivity(intent);
|
app.startActivity(intent);
|
||||||
} else {
|
|
||||||
// 如果是 release 版,就只发送一个通知
|
|
||||||
CrashHandleNotifyUtils.handleUncaughtException(app, intent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 发送一个通知
|
||||||
|
CrashHandleNotifyUtils.handleUncaughtException(app, intent);
|
||||||
|
|
||||||
// 终止当前进程(确保完全重启)
|
// 终止当前进程(确保完全重启)
|
||||||
android.os.Process.killProcess(android.os.Process.myPid());
|
android.os.Process.killProcess(android.os.Process.myPid());
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
@@ -440,9 +441,6 @@ public final class CrashHandler {
|
|||||||
// 设置系统默认主题(避免自定义主题冲突)
|
// 设置系统默认主题(避免自定义主题冲突)
|
||||||
setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
|
setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
|
||||||
|
|
||||||
// 判断是否为深色模式
|
|
||||||
boolean isNightMode = (getResources().getConfiguration().uiMode & android.content.res.Configuration.UI_MODE_NIGHT_MASK) == android.content.res.Configuration.UI_MODE_NIGHT_YES;
|
|
||||||
|
|
||||||
// 动态创建布局(避免 XML 布局加载异常)
|
// 动态创建布局(避免 XML 布局加载异常)
|
||||||
setContentView: {
|
setContentView: {
|
||||||
// 垂直滚动视图(处理日志过长)
|
// 垂直滚动视图(处理日志过长)
|
||||||
@@ -451,7 +449,7 @@ public final class CrashHandler {
|
|||||||
|
|
||||||
// 水平滚动视图(处理日志行过长)
|
// 水平滚动视图(处理日志行过长)
|
||||||
HorizontalScrollView hw = new HorizontalScrollView(this);
|
HorizontalScrollView hw = new HorizontalScrollView(this);
|
||||||
hw.setBackgroundColor(isNightMode ? 0xFF0D1B2A : 0xFFF5F5F5); // 深色模式灰色背景
|
hw.setBackgroundColor(0xFFF5F5F5); // 深色模式灰色背景
|
||||||
|
|
||||||
// 日志显示文本框
|
// 日志显示文本框
|
||||||
TextView message = new TextView(this);
|
TextView message = new TextView(this);
|
||||||
@@ -459,7 +457,7 @@ public final class CrashHandler {
|
|||||||
int padding = dp2px(16); // 内边距 16dp(适配不同屏幕)
|
int padding = dp2px(16); // 内边距 16dp(适配不同屏幕)
|
||||||
message.setPadding(padding, padding, padding, padding);
|
message.setPadding(padding, padding, padding, padding);
|
||||||
message.setText(mLog); // 设置崩溃日志
|
message.setText(mLog); // 设置崩溃日志
|
||||||
message.setTextColor(isNightMode ? 0xFFE0E0E0 : 0xFF000000); // 深色模式灰色文字,普通模式黑色文字
|
message.setTextColor(0xFF000000); // 深色模式灰色文字,普通模式黑色文字
|
||||||
message.setTextIsSelectable(true); // 支持文本选择(便于手动复制)
|
message.setTextIsSelectable(true); // 支持文本选择(便于手动复制)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,219 @@
|
|||||||
|
package cc.winboll.studio.libappbase;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
|
* @CreateTime 2026-05-11 13:28:00
|
||||||
|
* @EditTime 2026-05-11 13:30:45
|
||||||
|
* @Describe 应用崩溃保险丝单例管理类
|
||||||
|
* 限制短时间内应用重复崩溃,通过分级熔断机制控制崩溃页面跳转策略;
|
||||||
|
* 防护等级区间 1~2,每次崩溃自动降级,等级溢出则判定为彻底熔断;
|
||||||
|
* 支持本地文件持久化等级、延迟自动恢复、手动重置防护等级能力。
|
||||||
|
*/
|
||||||
|
public final class GlobalAppCrashSafetyWire {
|
||||||
|
|
||||||
|
/** 日志标识标签 */
|
||||||
|
public static final String TAG = "GlobalAppCrashSafetyWire";
|
||||||
|
/** 最低防护等级阈值 */
|
||||||
|
private static final int _MINI = 1;
|
||||||
|
/** 最高防护等级阈值 */
|
||||||
|
private static final int _MAX = 2;
|
||||||
|
|
||||||
|
/** 单例实例对象 */
|
||||||
|
private static volatile GlobalAppCrashSafetyWire _AppCrashSafetyWire;
|
||||||
|
/** 当前防护熔断等级 */
|
||||||
|
private volatile Integer currentSafetyLevel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私有构造方法
|
||||||
|
* 单例禁止外部实例化,初始化加载本地持久化防护等级
|
||||||
|
*/
|
||||||
|
private GlobalAppCrashSafetyWire() {
|
||||||
|
LogUtils.d(TAG, "构造方法执行:初始化崩溃保险丝实例");
|
||||||
|
currentSafetyLevel = loadCurrentSafetyLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单例对象
|
||||||
|
* @return GlobalAppCrashSafetyWire 单例
|
||||||
|
*/
|
||||||
|
public static synchronized GlobalAppCrashSafetyWire getInstance() {
|
||||||
|
if (_AppCrashSafetyWire == null) {
|
||||||
|
_AppCrashSafetyWire = new GlobalAppCrashSafetyWire();
|
||||||
|
}
|
||||||
|
return _AppCrashSafetyWire;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置当前防护等级
|
||||||
|
* @param currentSafetyLevel 待设置的防护等级
|
||||||
|
*/
|
||||||
|
public void setCurrentSafetyLevel(final int currentSafetyLevel) {
|
||||||
|
this.currentSafetyLevel = currentSafetyLevel;
|
||||||
|
LogUtils.d(TAG, "setCurrentSafetyLevel:设置等级 = " + currentSafetyLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前内存中防护等级
|
||||||
|
* @return 当前防护等级
|
||||||
|
*/
|
||||||
|
public int getCurrentSafetyLevel() {
|
||||||
|
return currentSafetyLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存防护等级到本地文件持久化
|
||||||
|
* @param currentSafetyLevel 待保存防护等级
|
||||||
|
*/
|
||||||
|
public void saveCurrentSafetyLevel(final int currentSafetyLevel) {
|
||||||
|
LogUtils.d(TAG, "saveCurrentSafetyLevel:开始持久化等级 = " + currentSafetyLevel);
|
||||||
|
this.currentSafetyLevel = currentSafetyLevel;
|
||||||
|
|
||||||
|
ObjectOutputStream oos = null;
|
||||||
|
try {
|
||||||
|
oos = new ObjectOutputStream(new FileOutputStream(CrashHandler._CrashCountFilePath));
|
||||||
|
oos.writeInt(currentSafetyLevel);
|
||||||
|
oos.flush();
|
||||||
|
LogUtils.d(TAG, "saveCurrentSafetyLevel:写入文件成功,等级 = " + currentSafetyLevel);
|
||||||
|
} catch (IOException e) {
|
||||||
|
LogUtils.e(TAG, "saveCurrentSafetyLevel:写入文件异常", e);
|
||||||
|
} finally {
|
||||||
|
if (oos != null) {
|
||||||
|
try {
|
||||||
|
oos.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LogUtils.e(TAG, "saveCurrentSafetyLevel:关闭流异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从本地文件读取防护等级
|
||||||
|
* @return 加载后的防护等级
|
||||||
|
*/
|
||||||
|
public int loadCurrentSafetyLevel() {
|
||||||
|
LogUtils.d(TAG, "loadCurrentSafetyLevel:加载本地防护等级");
|
||||||
|
File file = new File(CrashHandler._CrashCountFilePath);
|
||||||
|
|
||||||
|
if (!file.exists()) {
|
||||||
|
currentSafetyLevel = _MAX;
|
||||||
|
LogUtils.d(TAG, "loadCurrentSafetyLevel:文件不存在,初始化为最高等级 = " + _MAX);
|
||||||
|
saveCurrentSafetyLevel(currentSafetyLevel);
|
||||||
|
return currentSafetyLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
ObjectInputStream ois = null;
|
||||||
|
try {
|
||||||
|
ois = new ObjectInputStream(new FileInputStream(CrashHandler._CrashCountFilePath));
|
||||||
|
currentSafetyLevel = ois.readInt();
|
||||||
|
LogUtils.d(TAG, "loadCurrentSafetyLevel:读取文件成功,当前等级 = " + currentSafetyLevel);
|
||||||
|
} catch (IOException e) {
|
||||||
|
LogUtils.e(TAG, "loadCurrentSafetyLevel:读取文件异常", e);
|
||||||
|
currentSafetyLevel = _MAX;
|
||||||
|
} finally {
|
||||||
|
if (ois != null) {
|
||||||
|
try {
|
||||||
|
ois.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LogUtils.e(TAG, "loadCurrentSafetyLevel:关闭流异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentSafetyLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行一次保险丝熔断降级
|
||||||
|
* @return 降级后是否仍在有效防护区间
|
||||||
|
*/
|
||||||
|
boolean burnSafetyWire() {
|
||||||
|
LogUtils.d(TAG, "burnSafetyWire:执行一次熔断降级");
|
||||||
|
final int safeLevel = loadCurrentSafetyLevel();
|
||||||
|
|
||||||
|
if (isSafetyWireWorking(safeLevel)) {
|
||||||
|
saveCurrentSafetyLevel(safeLevel - 1);
|
||||||
|
return isSafetyWireWorking(safeLevel - 1);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验指定等级是否在有效防护区间
|
||||||
|
* @param safetyLevel 待校验防护等级
|
||||||
|
* @return true=防护有效 false=已熔断
|
||||||
|
*/
|
||||||
|
boolean isSafetyWireWorking(final int safetyLevel) {
|
||||||
|
LogUtils.d(TAG, "isSafetyWireWorking:校验等级 = " + safetyLevel);
|
||||||
|
if (safetyLevel >= _MINI && safetyLevel <= _MAX) {
|
||||||
|
LogUtils.d(TAG, "isSafetyWireWorking:等级在合法区间内");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
LogUtils.d(TAG, "isSafetyWireWorking:等级超出合法区间,已熔断");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 立即恢复防护等级为最高等级
|
||||||
|
*/
|
||||||
|
void resumeToMaximumImmediately() {
|
||||||
|
LogUtils.d(TAG, "resumeToMaximumImmediately:重置为最高防护等级");
|
||||||
|
GlobalAppCrashSafetyWire.getInstance().saveCurrentSafetyLevel(_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭防护,设置为临界最低等级
|
||||||
|
*/
|
||||||
|
void off() {
|
||||||
|
LogUtils.d(TAG, "off:设置为临界最低防护等级");
|
||||||
|
saveCurrentSafetyLevel(_MINI);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前整体保险丝是否处于可用防护状态
|
||||||
|
* @return true=防护正常 false=已熔断
|
||||||
|
*/
|
||||||
|
boolean isAppCrashSafetyWireOK() {
|
||||||
|
LogUtils.d(TAG, "isAppCrashSafetyWireOK:检测当前防护状态");
|
||||||
|
currentSafetyLevel = loadCurrentSafetyLevel();
|
||||||
|
return isSafetyWireWorking(currentSafetyLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 延迟异步检测并自动恢复防护等级
|
||||||
|
* @param context 上下文对象
|
||||||
|
*/
|
||||||
|
void postResumeCrashSafetyWireHandler(final Context context) {
|
||||||
|
LogUtils.d(TAG, "postResumeCrashSafetyWireHandler:开启延迟自动恢复任务");
|
||||||
|
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
final int nextLevel = currentSafetyLevel - 1;
|
||||||
|
if (!GlobalAppCrashSafetyWire.getInstance().isSafetyWireWorking(nextLevel)) {
|
||||||
|
GlobalAppCrashSafetyWire.getInstance().resumeToMaximumImmediately();
|
||||||
|
LogUtils.d(TAG, "postResumeCrashSafetyWireHandler:临近熔断,自动重置最高等级");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testGlobalAppCrashSafetyWire(Context context) {
|
||||||
|
if (GlobalAppCrashSafetyWire.getInstance().isAppCrashSafetyWireOK()) {
|
||||||
|
GlobalAppCrashSafetyWire.getInstance().burnSafetyWire();
|
||||||
|
for (int i = Integer.MIN_VALUE; i < Integer.MAX_VALUE; i++) {
|
||||||
|
context.getString(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -9,178 +9,127 @@ import android.content.pm.PackageManager;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import cc.winboll.studio.libappbase.R;
|
import cc.winboll.studio.libappbase.R;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
* @Date 2025/11/11 19:58
|
* @CreateTime 2025-11-11 19:58:00
|
||||||
|
* @EditTime 2025-11-11 20:15:32
|
||||||
* @Describe 应用异常报告观察活动窗口类
|
* @Describe 应用异常报告观察活动窗口类
|
||||||
* 核心功能:应用发生未捕获崩溃时,由 CrashHandler 启动此页面,展示崩溃日志详情,
|
* 应用发生未捕获崩溃时,由 CrashHandler 启动此页面,展示崩溃日志详情,
|
||||||
* 并提供「复制日志」「重启应用」操作入口,便于开发者定位问题和用户恢复应用
|
* 提供复制日志、重启应用操作入口,便于开发者定位问题及用户恢复应用;
|
||||||
|
* 页面初始化异常时捕获错误并输出调试日志,提升崩溃页面自身稳定性。
|
||||||
*/
|
*/
|
||||||
public final class GlobalCrashActivity extends Activity implements MenuItem.OnMenuItemClickListener {
|
public final class GlobalCrashActivity extends Activity
|
||||||
|
implements MenuItem.OnMenuItemClickListener {
|
||||||
|
|
||||||
/** 日志标签(用于调试日志输出,唯一标识当前 Activity) */
|
/** 日志标签 */
|
||||||
public static final String TAG = "GlobalCrashActivity";
|
public static final String TAG = "GlobalCrashActivity";
|
||||||
|
|
||||||
/** 菜单标识:复制崩溃日志(用于区分菜单项点击事件) */
|
/** 菜单标识:复制崩溃日志 */
|
||||||
private static final int MENU_ITEM_COPY = 0;
|
private static final int MENU_ITEM_COPY = 0;
|
||||||
/** 菜单标识:重启应用(用于区分菜单项点击事件) */
|
/** 菜单标识:重启应用 */
|
||||||
private static final int MENU_ITEM_RESTART = 1;
|
private static final int MENU_ITEM_RESTART = 1;
|
||||||
|
|
||||||
/** 崩溃报告展示自定义视图 */
|
/** 崩溃报告自定义视图 */
|
||||||
// 负责渲染崩溃日志文本、提供 Toolbar 容器,封装了日志展示和菜单样式控制逻辑
|
|
||||||
private GlobalCrashReportView mCrashReportView;
|
private GlobalCrashReportView mCrashReportView;
|
||||||
|
/** 崩溃日志文本 */
|
||||||
/** 崩溃日志文本内容 */
|
|
||||||
// 从 CrashHandler 通过 Intent 传递过来,包含异常堆栈、设备信息等完整崩溃数据
|
|
||||||
private String mCrashLog;
|
private String mCrashLog;
|
||||||
|
|
||||||
/**
|
|
||||||
* Activity 创建时初始化(生命周期核心方法,仅执行一次)
|
|
||||||
* @param savedInstanceState 保存的实例状态(崩溃页面无需恢复状态,此处仅作兼容)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
LogUtils.d(TAG, "onCreate: 初始化崩溃展示页面");
|
||||||
|
|
||||||
// 初始化崩溃安全防护机制
|
final Context appContext = getApplicationContext();
|
||||||
// 作用:防止应用重启后短时间内再次崩溃,由 CrashHandler 内部实现防护逻辑
|
GlobalAppCrashSafetyWire.getInstance()
|
||||||
CrashHandler.AppCrashSafetyWire.getInstance()
|
.postResumeCrashSafetyWireHandler(appContext);
|
||||||
.postResumeCrashSafetyWireHandler(getApplicationContext());
|
|
||||||
|
|
||||||
// 从 Intent 中获取崩溃日志数据(EXTRA_CRASH_INFO 为 CrashHandler 定义的常量键)
|
mCrashLog = getIntent().getStringExtra(CrashHandler.EXTRA_CRASH_LOG);
|
||||||
mCrashLog = getIntent().getStringExtra(CrashHandler.EXTRA_CRASH_LOG);
|
setContentView(R.layout.activity_globalcrash);
|
||||||
|
|
||||||
// 设置当前 Activity 的布局文件(展示崩溃报告的 UI 结构)
|
mCrashReportView = findViewById(R.id.activityglobalcrashGlobalCrashReportView1);
|
||||||
setContentView(R.layout.activity_globalcrash);
|
if (mCrashReportView != null) {
|
||||||
|
mCrashReportView.setReport(mCrashLog);
|
||||||
|
setActionBar(mCrashReportView.getToolbar());
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化崩溃报告展示视图(通过布局 ID 找到自定义 View 实例)
|
if (getActionBar() != null) {
|
||||||
mCrashReportView = findViewById(R.id.activityglobalcrashGlobalCrashReportView1);
|
getActionBar().setTitle(CrashHandler.TITTLE);
|
||||||
// 将崩溃日志设置到视图中,由自定义 View 负责排版和显示
|
final String appName = GlobalApplication.getAppName(appContext);
|
||||||
mCrashReportView.setReport(mCrashLog);
|
getActionBar().setSubtitle(appName);
|
||||||
|
}
|
||||||
// 设置页面的 ActionBar(复用自定义 View 中的 Toolbar 作为系统 ActionBar)
|
LogUtils.d(TAG, "onCreate: 崩溃页面初始化完成");
|
||||||
setActionBar(mCrashReportView.getToolbar());
|
|
||||||
|
|
||||||
// 配置 ActionBar 标题和副标题(非空判断避免空指针异常)
|
|
||||||
if (getActionBar() != null) {
|
|
||||||
// 设置标题:使用 CrashHandler 中定义的统一标题(如 "应用崩溃报告")
|
|
||||||
getActionBar().setTitle(CrashHandler.TITTLE);
|
|
||||||
// 设置副标题:显示当前应用名称(从全局 Application 工具方法获取)
|
|
||||||
getActionBar().setSubtitle(GlobalApplication.getAppName(getApplicationContext()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 重写返回键点击事件
|
|
||||||
* 逻辑:点击手机返回键时,直接重启应用(而非返回上一页,因崩溃后上一页状态可能异常)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
restartApp();
|
restartApp();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重启当前应用(核心工具方法)
|
* 重启当前应用
|
||||||
* 实现逻辑:
|
|
||||||
* 1. 获取应用的启动意图(默认启动 AndroidManifest 中配置的主 Activity)
|
|
||||||
* 2. 设置意图标志,清除原有任务栈,避免残留异常页面
|
|
||||||
* 3. 启动主 Activity 并终止当前进程,确保应用完全重启
|
|
||||||
*/
|
*/
|
||||||
private void restartApp() {
|
private void restartApp() {
|
||||||
// 获取 PackageManager 实例(用于获取应用相关信息和意图)
|
LogUtils.d(TAG, "restartApp: 执行应用重启操作");
|
||||||
PackageManager packageManager = getPackageManager();
|
final PackageManager packageManager = getPackageManager();
|
||||||
// 获取应用的启动意图(参数为当前应用包名,返回主 Activity 的意图)
|
final Intent launchIntent = packageManager.getLaunchIntentForPackage(getPackageName());
|
||||||
Intent launchIntent = packageManager.getLaunchIntentForPackage(getPackageName());
|
|
||||||
|
|
||||||
if (launchIntent != null) {
|
if (launchIntent != null) {
|
||||||
// 设置意图标志:
|
|
||||||
// FLAG_ACTIVITY_NEW_TASK:创建新的任务栈启动 Activity
|
|
||||||
// FLAG_ACTIVITY_CLEAR_TOP:清除目标 Activity 之上的所有 Activity
|
|
||||||
// FLAG_ACTIVITY_CLEAR_TASK:清除当前任务栈中的所有 Activity
|
|
||||||
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
| Intent.FLAG_ACTIVITY_CLEAR_TOP
|
| Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||||
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
// 启动应用主 Activity
|
|
||||||
startActivity(launchIntent);
|
startActivity(launchIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关闭当前崩溃报告页面
|
|
||||||
finish();
|
finish();
|
||||||
// 终止当前应用进程(确保释放所有资源,避免内存泄漏)
|
|
||||||
android.os.Process.killProcess(android.os.Process.myPid());
|
android.os.Process.killProcess(android.os.Process.myPid());
|
||||||
// 强制退出虚拟机(彻底终止应用,防止残留线程继续运行)
|
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 菜单项点击事件回调(实现 MenuItem.OnMenuItemClickListener 接口)
|
|
||||||
* @param item 被点击的菜单项实例
|
|
||||||
* @return boolean:true 表示事件已消费,不再向下传递;false 表示未消费
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMenuItemClick(MenuItem item) {
|
public boolean onMenuItemClick(MenuItem item) {
|
||||||
// 根据菜单项 ID 判断点击的是哪个功能
|
final int itemId = item.getItemId();
|
||||||
switch (item.getItemId()) {
|
switch (itemId) {
|
||||||
case MENU_ITEM_COPY:
|
case MENU_ITEM_COPY:
|
||||||
// 点击「复制」菜单,执行复制崩溃日志到剪贴板
|
|
||||||
copyCrashLogToClipboard();
|
copyCrashLogToClipboard();
|
||||||
break;
|
break;
|
||||||
case MENU_ITEM_RESTART:
|
case MENU_ITEM_RESTART:
|
||||||
// 点击「重启」菜单:先恢复崩溃防护机制到最大等级,再重启应用
|
GlobalAppCrashSafetyWire.getInstance().resumeToMaximumImmediately();
|
||||||
CrashHandler.AppCrashSafetyWire.getInstance().resumeToMaximumImmediately();
|
|
||||||
restartApp();
|
restartApp();
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建页面顶部菜单(ActionBar 菜单)
|
|
||||||
* @param menu 菜单容器,用于添加菜单项
|
|
||||||
* @return boolean:true 表示显示菜单;false 表示不显示
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
// 添加「复制」菜单项:
|
|
||||||
// 参数说明:菜单组 ID(0 表示默认组)、菜单项 ID(MENU_ITEM_COPY)、排序号(0)、菜单文本("Copy")
|
|
||||||
// setOnMenuItemClickListener(this):绑定点击事件到当前 Activity
|
|
||||||
// setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM):有空间时显示在 ActionBar 上,否则放入溢出菜单
|
|
||||||
menu.add(0, MENU_ITEM_COPY, 0, "Copy")
|
menu.add(0, MENU_ITEM_COPY, 0, "Copy")
|
||||||
.setOnMenuItemClickListener(this)
|
.setOnMenuItemClickListener(this)
|
||||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||||
|
|
||||||
// 添加「重启」菜单项(参数含义同上)
|
|
||||||
menu.add(0, MENU_ITEM_RESTART, 0, "Restart")
|
menu.add(0, MENU_ITEM_RESTART, 0, "Restart")
|
||||||
.setOnMenuItemClickListener(this)
|
.setOnMenuItemClickListener(this)
|
||||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||||
|
|
||||||
// 调用自定义视图的方法,更新菜单文字样式(如颜色、字体大小等,由自定义 View 内部实现)
|
if (mCrashReportView != null) {
|
||||||
mCrashReportView.updateMenuStyle();
|
mCrashReportView.updateMenuStyle();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将崩溃日志复制到系统剪贴板(工具方法)
|
* 复制崩溃日志到系统剪贴板
|
||||||
* 功能:用户点击复制菜单后,将完整崩溃日志存入剪贴板,方便粘贴到聊天工具或文档中
|
|
||||||
*/
|
*/
|
||||||
private void copyCrashLogToClipboard() {
|
private void copyCrashLogToClipboard() {
|
||||||
// 获取系统剪贴板服务(需通过 getSystemService 方法获取)
|
LogUtils.d(TAG, "copyCrashLogToClipboard: 复制崩溃日志到剪贴板");
|
||||||
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
final ClipboardManager clipboardManager = (ClipboardManager)
|
||||||
|
getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
// 创建剪贴板数据:
|
final ClipData clipData = ClipData.newPlainText(getPackageName(), mCrashLog);
|
||||||
// 参数 1:标签(用于标识剪贴板内容来源,此处用应用包名)
|
|
||||||
// 参数 2:实际复制的文本内容(崩溃日志)
|
|
||||||
ClipData clipData = ClipData.newPlainText(getPackageName(), mCrashLog);
|
|
||||||
|
|
||||||
// 将数据设置到剪贴板(完成复制操作)
|
|
||||||
clipboardManager.setPrimaryClip(clipData);
|
clipboardManager.setPrimaryClip(clipData);
|
||||||
|
|
||||||
// 显示复制成功的 Toast 提示(告知用户操作结果)
|
|
||||||
Toast.makeText(getApplication(), "The text is copied.", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getApplication(), "The text is copied.", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package cc.winboll.studio.libappbase;
|
package cc.winboll.studio.libappbase;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.widget.Toast;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||||
import dalvik.system.DexFile;
|
import dalvik.system.DexFile;
|
||||||
@@ -28,28 +28,33 @@ import java.util.Map;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
* @CreateTime 2026/05/09 15:46:28
|
* @CreateTime 2026-05-09 15:46:28
|
||||||
* @LastEditTime 2026/05/09 17:28:45
|
* @EditTime 2026-05-11 15:36:12
|
||||||
* @Describe 应用日志工具类
|
* @Describe 应用日志工具类
|
||||||
* 补全多级别日志重载、自动日志文件裁剪、应用内TAG自动扫描管理
|
* 补全多级别日志重载、自动日志文件裁剪、应用内TAG自动扫描管理;
|
||||||
* 支持日志本地持久化、异常堆栈格式化、TAG开关配置、线程/集合打印工具
|
* 支持日志本地持久化、异常堆栈格式化、TAG开关配置、线程与集合打印工具;
|
||||||
* 完全兼容Java7语法,严格遵循变量final编码规范
|
* 完全兼容Java7语法,严格遵循变量final编码规范;
|
||||||
* 重要说明:本类内部调试打印必须使用 android.util.Log,禁止使用LogUtils自身方法;
|
* 重要说明:本类内部调试打印必须使用 android.util.Log,禁止使用LogUtils自身方法,
|
||||||
* 若内部使用LogUtils打印会造成递归嵌套调用,程序运行时会概率出现逻辑漩涡、循环无限制调用问题。
|
* 避免递归嵌套调用、逻辑漩涡与无限循环调用问题。
|
||||||
*/
|
*/
|
||||||
public class LogUtils {
|
public class LogUtils {
|
||||||
|
|
||||||
// ====================== 常量与枚举定义 ======================
|
// ====================== 常量与枚举 ======================
|
||||||
public static final String TAG = "LogUtils";
|
public static final String TAG = "LogUtils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志级别枚举
|
* 日志级别枚举
|
||||||
*/
|
*/
|
||||||
public static enum LOG_LEVEL {
|
public static enum LOG_LEVEL {
|
||||||
Off, Error, Warn, Info, Debug, Verbose
|
Off,
|
||||||
|
Error,
|
||||||
|
Warn,
|
||||||
|
Info,
|
||||||
|
Debug,
|
||||||
|
Verbose
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================== 全局成员变量 ======================
|
// ====================== 全局静态成员 ======================
|
||||||
private static volatile boolean _IsInited = false;
|
private static volatile boolean _IsInited = false;
|
||||||
private static Context _mContext;
|
private static Context _mContext;
|
||||||
private static final SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("[yyyyMMdd_HHmmss_SSS]", Locale.getDefault());
|
private static final SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("[yyyyMMdd_HHmmss_SSS]", Locale.getDefault());
|
||||||
@@ -60,20 +65,11 @@ public class LogUtils {
|
|||||||
private static LogUtilsBean _mLogUtilsBean;
|
private static LogUtilsBean _mLogUtilsBean;
|
||||||
public static final Map<String, Boolean> mapTAGList = new HashMap<String, Boolean>();
|
public static final Map<String, Boolean> mapTAGList = new HashMap<String, Boolean>();
|
||||||
|
|
||||||
// ====================== 初始化入口方法 ======================
|
// ====================== 初始化入口 ======================
|
||||||
/**
|
|
||||||
* 初始化日志工具,默认级别Off
|
|
||||||
* @param context 上下文
|
|
||||||
*/
|
|
||||||
public static void init(final Context context) {
|
public static void init(final Context context) {
|
||||||
init(context, LOG_LEVEL.Off);
|
init(context, LOG_LEVEL.Off);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化日志工具,指定日志级别
|
|
||||||
* @param context 上下文
|
|
||||||
* @param logLevel 日志级别
|
|
||||||
*/
|
|
||||||
public static void init(final Context context, final LOG_LEVEL logLevel) {
|
public static void init(final Context context, final LOG_LEVEL logLevel) {
|
||||||
Log.d(TAG, "init 执行日志工具初始化");
|
Log.d(TAG, "init 执行日志工具初始化");
|
||||||
_mContext = context;
|
_mContext = context;
|
||||||
@@ -93,10 +89,7 @@ public class LogUtils {
|
|||||||
Log.d(TAG, "init 日志工具初始化完成");
|
Log.d(TAG, "init 日志工具初始化完成");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================== 目录初始化私有方法 ======================
|
// ====================== 目录初始化 ======================
|
||||||
/**
|
|
||||||
* 调试模式初始化外部存储日志目录
|
|
||||||
*/
|
|
||||||
private static void initDebugDir() {
|
private static void initDebugDir() {
|
||||||
final Context appContext = _mContext.getApplicationContext();
|
final Context appContext = _mContext.getApplicationContext();
|
||||||
_mfLogCacheDir = new File(appContext.getExternalCacheDir(), TAG);
|
_mfLogCacheDir = new File(appContext.getExternalCacheDir(), TAG);
|
||||||
@@ -112,9 +105,6 @@ public class LogUtils {
|
|||||||
_mfLogUtilsBeanFile = new File(_mfLogDataDir, TAG + ".json");
|
_mfLogUtilsBeanFile = new File(_mfLogDataDir, TAG + ".json");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 正式模式初始化内部存储日志目录
|
|
||||||
*/
|
|
||||||
private static void initReleaseDir() {
|
private static void initReleaseDir() {
|
||||||
final Context appContext = _mContext.getApplicationContext();
|
final Context appContext = _mContext.getApplicationContext();
|
||||||
_mfLogCacheDir = new File(appContext.getCacheDir(), TAG);
|
_mfLogCacheDir = new File(appContext.getCacheDir(), TAG);
|
||||||
@@ -130,9 +120,6 @@ public class LogUtils {
|
|||||||
_mfLogUtilsBeanFile = new File(_mfLogDataDir, TAG + ".json");
|
_mfLogUtilsBeanFile = new File(_mfLogDataDir, TAG + ".json");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化日志配置Bean,无配置则自动创建默认配置
|
|
||||||
*/
|
|
||||||
private static void initLogConfigBean() {
|
private static void initLogConfigBean() {
|
||||||
_mLogUtilsBean = LogUtilsBean.loadBeanFromFile(_mfLogUtilsBeanFile.getPath(), LogUtilsBean.class);
|
_mLogUtilsBean = LogUtilsBean.loadBeanFromFile(_mfLogUtilsBeanFile.getPath(), LogUtilsBean.class);
|
||||||
if (_mLogUtilsBean == null) {
|
if (_mLogUtilsBean == null) {
|
||||||
@@ -142,10 +129,7 @@ public class LogUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================== 日志文件裁剪(Java7 兼容) ======================
|
// ====================== 日志文件裁剪 ======================
|
||||||
/**
|
|
||||||
* 日志文件大小检查裁剪,超6MB保留最新3MB
|
|
||||||
*/
|
|
||||||
private static void checkAndTrimLogFileSize() {
|
private static void checkAndTrimLogFileSize() {
|
||||||
if (_mfLogCatchFile == null || !_mfLogCatchFile.exists()) {
|
if (_mfLogCatchFile == null || !_mfLogCatchFile.exists()) {
|
||||||
return;
|
return;
|
||||||
@@ -212,14 +196,11 @@ public class LogUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================== TAG配置管理 ======================
|
// ====================== TAG 配置管理 ======================
|
||||||
public static Map<String, Boolean> getMapTAGList() {
|
public static Map<String, Boolean> getMapTAGList() {
|
||||||
return mapTAGList;
|
return mapTAGList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 加载本地TAG启用配置
|
|
||||||
*/
|
|
||||||
private static void loadTAGBeanSettings() {
|
private static void loadTAGBeanSettings() {
|
||||||
final ArrayList<LogUtilsClassTAGBean> list = new ArrayList<LogUtilsClassTAGBean>();
|
final ArrayList<LogUtilsClassTAGBean> list = new ArrayList<LogUtilsClassTAGBean>();
|
||||||
LogUtilsClassTAGBean.loadBeanList(_mContext, list, LogUtilsClassTAGBean.class);
|
LogUtilsClassTAGBean.loadBeanList(_mContext, list, LogUtilsClassTAGBean.class);
|
||||||
@@ -232,9 +213,6 @@ public class LogUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 保存当前TAG开关配置到本地
|
|
||||||
*/
|
|
||||||
private static void saveTAGBeanSettings() {
|
private static void saveTAGBeanSettings() {
|
||||||
final ArrayList<LogUtilsClassTAGBean> list = new ArrayList<LogUtilsClassTAGBean>();
|
final ArrayList<LogUtilsClassTAGBean> list = new ArrayList<LogUtilsClassTAGBean>();
|
||||||
for (final Map.Entry<String, Boolean> entry : mapTAGList.entrySet()) {
|
for (final Map.Entry<String, Boolean> entry : mapTAGList.entrySet()) {
|
||||||
@@ -243,9 +221,6 @@ public class LogUtils {
|
|||||||
LogUtilsClassTAGBean.saveBeanList(_mContext, list, LogUtilsClassTAGBean.class);
|
LogUtilsClassTAGBean.saveBeanList(_mContext, list, LogUtilsClassTAGBean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 自动扫描应用内含 public static String TAG 的类
|
|
||||||
*/
|
|
||||||
private static void addClassTAGList() {
|
private static void addClassTAGList() {
|
||||||
try {
|
try {
|
||||||
final String packageNamePrefix = "cc.winboll.studio";
|
final String packageNamePrefix = "cc.winboll.studio";
|
||||||
@@ -277,8 +252,12 @@ public class LogUtils {
|
|||||||
mapTAGList.put(tagValue, false);
|
mapTAGList.put(tagValue, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (NoClassDefFoundError | ClassNotFoundException | IllegalAccessException e) {
|
} catch (NoClassDefFoundError e) {
|
||||||
Log.w(TAG, "addClassTAGList 解析类TAG失败");
|
Log.w(TAG, "addClassTAGList 解析类TAG失败 NoClassDefFoundError");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
Log.w(TAG, "addClassTAGList 解析类TAG失败 ClassNotFoundException");
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
Log.w(TAG, "addClassTAGList 解析类TAG失败 IllegalAccessException");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -286,11 +265,6 @@ public class LogUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置单个TAG日志开关
|
|
||||||
* @param tag 日志TAG
|
|
||||||
* @param isEnable 是否启用
|
|
||||||
*/
|
|
||||||
public static void setTAGListEnable(final String tag, final boolean isEnable) {
|
public static void setTAGListEnable(final String tag, final boolean isEnable) {
|
||||||
final Iterator<Map.Entry<String, Boolean>> iterator = mapTAGList.entrySet().iterator();
|
final Iterator<Map.Entry<String, Boolean>> iterator = mapTAGList.entrySet().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
@@ -304,10 +278,6 @@ public class LogUtils {
|
|||||||
Log.d(TAG, "setTAGListEnable 更新TAG开关配置");
|
Log.d(TAG, "setTAGListEnable 更新TAG开关配置");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 批量设置所有TAG日志开关
|
|
||||||
* @param isEnable 是否全量启用
|
|
||||||
*/
|
|
||||||
public static void setALlTAGListEnable(final boolean isEnable) {
|
public static void setALlTAGListEnable(final boolean isEnable) {
|
||||||
for (final Map.Entry<String, Boolean> entry : mapTAGList.entrySet()) {
|
for (final Map.Entry<String, Boolean> entry : mapTAGList.entrySet()) {
|
||||||
entry.setValue(isEnable);
|
entry.setValue(isEnable);
|
||||||
@@ -326,12 +296,6 @@ public class LogUtils {
|
|||||||
return _mLogUtilsBean.getLogLevel();
|
return _mLogUtilsBean.getLogLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断当前TAG及级别是否允许输出日志
|
|
||||||
* @param tag 日志标识
|
|
||||||
* @param logLevel 日志级别
|
|
||||||
* @return 是否允许打印
|
|
||||||
*/
|
|
||||||
private static boolean isLoggable(final String tag, final LOG_LEVEL logLevel) {
|
private static boolean isLoggable(final String tag, final LOG_LEVEL logLevel) {
|
||||||
if (!_IsInited) {
|
if (!_IsInited) {
|
||||||
return false;
|
return false;
|
||||||
@@ -342,16 +306,11 @@ public class LogUtils {
|
|||||||
return isInTheLevel(logLevel);
|
return isInTheLevel(logLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断日志级别是否匹配输出等级
|
|
||||||
* @param logLevel 待判断级别
|
|
||||||
* @return 是否达标
|
|
||||||
*/
|
|
||||||
private static boolean isInTheLevel(final LOG_LEVEL logLevel) {
|
private static boolean isInTheLevel(final LOG_LEVEL logLevel) {
|
||||||
return _mLogUtilsBean.getLogLevel().ordinal() >= logLevel.ordinal();
|
return _mLogUtilsBean.getLogLevel().ordinal() >= logLevel.ordinal();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================== 对外基础工具方法 ======================
|
// ====================== 基础对外方法 ======================
|
||||||
public static File getLogCacheDir() {
|
public static File getLogCacheDir() {
|
||||||
return _mfLogCacheDir;
|
return _mfLogCacheDir;
|
||||||
}
|
}
|
||||||
@@ -360,7 +319,7 @@ public class LogUtils {
|
|||||||
return _IsInited;
|
return _IsInited;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================== Error 日志重载 ======================
|
// ====================== Error 日志重载(补齐缺失方法) ======================
|
||||||
public static void e(final String szTAG, final String szMessage) {
|
public static void e(final String szTAG, final String szMessage) {
|
||||||
if (isLoggable(szTAG, LOG_LEVEL.Error)) {
|
if (isLoggable(szTAG, LOG_LEVEL.Error)) {
|
||||||
saveLog(szTAG, LOG_LEVEL.Error, szMessage);
|
saveLog(szTAG, LOG_LEVEL.Error, szMessage);
|
||||||
@@ -391,6 +350,17 @@ public class LogUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 补齐你需要的:LogUtils.e(TAG, "uncaughtException: 崩溃处理异常", e)
|
||||||
|
*/
|
||||||
|
public static void e(final String szTAG, final String szMessage, final Throwable e) {
|
||||||
|
if (isLoggable(szTAG, LOG_LEVEL.Error)) {
|
||||||
|
final StringBuilder sb = new StringBuilder(szMessage);
|
||||||
|
sb.append("\n【异常信息】: ").append(getThrowableInfo(e));
|
||||||
|
saveLog(szTAG, LOG_LEVEL.Error, sb.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ====================== Warn 日志重载 ======================
|
// ====================== Warn 日志重载 ======================
|
||||||
public static void w(final String szTAG, final String szMessage) {
|
public static void w(final String szTAG, final String szMessage) {
|
||||||
if (isLoggable(szTAG, LOG_LEVEL.Warn)) {
|
if (isLoggable(szTAG, LOG_LEVEL.Warn)) {
|
||||||
@@ -560,6 +530,24 @@ public class LogUtils {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getThrowableInfo(final Throwable e) {
|
||||||
|
if (e == null) {
|
||||||
|
return "异常对象为null";
|
||||||
|
}
|
||||||
|
final StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(e.getClass().getSimpleName()).append(" : ")
|
||||||
|
.append(e.getMessage() == null ? "无异常消息" : e.getMessage());
|
||||||
|
|
||||||
|
final StackTraceElement[] stackTrace = e.getStackTrace();
|
||||||
|
if (stackTrace != null && stackTrace.length > 0) {
|
||||||
|
final int limit = Math.min(stackTrace.length, 5);
|
||||||
|
for (int i = 0; i < limit; i++) {
|
||||||
|
sb.append("\n ").append(stackTrace[i].toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static String getStackTraceInfo(final StackTraceElement[] stackTrace) {
|
private static String getStackTraceInfo(final StackTraceElement[] stackTrace) {
|
||||||
if (stackTrace == null || stackTrace.length == 0) {
|
if (stackTrace == null || stackTrace.length == 0) {
|
||||||
return "堆栈信息为空";
|
return "堆栈信息为空";
|
||||||
@@ -646,15 +634,23 @@ public class LogUtils {
|
|||||||
Log.d(TAG, "cleanLog 日志文件未初始化");
|
Log.d(TAG, "cleanLog 日志文件未初始化");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
BufferedWriter out = null;
|
||||||
try {
|
try {
|
||||||
final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
out = new BufferedWriter(new OutputStreamWriter(
|
||||||
new FileOutputStream(_mfLogCatchFile, false), "UTF-8"));
|
new FileOutputStream(_mfLogCatchFile, false), "UTF-8"));
|
||||||
out.write("");
|
out.write("");
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
|
||||||
Log.d(TAG, "cleanLog 日志已清空");
|
Log.d(TAG, "cleanLog 日志已清空");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "cleanLog 清空日志失败", e);
|
Log.e(TAG, "cleanLog 清空日志失败", e);
|
||||||
|
} finally {
|
||||||
|
if (out != null) {
|
||||||
|
try {
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "cleanLog 流关闭异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -674,10 +670,11 @@ public class LogUtils {
|
|||||||
if (Thread.currentThread().getId() == android.os.Process.myTid()) {
|
if (Thread.currentThread().getId() == android.os.Process.myTid()) {
|
||||||
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
||||||
} else {
|
} else {
|
||||||
((android.app.Activity) context).runOnUiThread(new Runnable() {
|
final Context uiContext = context;
|
||||||
|
((android.app.Activity) uiContext).runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
Toast.makeText(uiContext, message, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
<LinearLayout
|
<LinearLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="?attr/activityBackgroundColor">>
|
android:background="?attr/activityBackgroundColor">
|
||||||
|
|
||||||
<cc.winboll.studio.libappbase.GlobalCrashReportView
|
<cc.winboll.studio.libappbase.GlobalCrashReportView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|||||||
Reference in New Issue
Block a user