正在改造WinBoLL主机切换对话框与按钮。。。

This commit is contained in:
2026-01-22 21:19:39 +08:00
parent 5846784940
commit d20192cb36
8 changed files with 235 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package cc.winboll.studio.libappbase;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
@@ -26,6 +27,12 @@ public class GlobalApplication extends Application {
*/
private static volatile boolean isDebugging = false;
// 新增WinBoLL 服务器主机地址volatile 保证多线程可见性)
private static volatile String winbollHost = null;
// 新增SP 存储相关常量(私有存储,仅当前应用可访问)
private static final String SP_NAME = "WinBoLL_SP_CONFIG";
private static final String SP_KEY_WINBOLL_HOST = "winboll_host";
/**
* 获取全局 Application 单例实例(外部可通过此方法获取上下文)
* @return GlobalApplication 单例(未初始化时返回 null需确保配置 AndroidManifest
@@ -53,7 +60,7 @@ public class GlobalApplication extends Application {
}
// 将调试状态封装为 APPModel 并保存到文件
APPModel.saveBeanToFile(
getAppModelFilePath(application),
getAppModelFilePath(application),
new APPModel(isDebugging)
);
}
@@ -76,6 +83,37 @@ public class GlobalApplication extends Application {
return isDebugging;
}
// 新增:设置 WinBoLL 服务器主机地址(同时保存到 SP 持久化)
public static void setWinbollHost(String host) {
if (sInstance == null) {
LogUtils.e(TAG, "setWinbollHost: 应用未初始化,设置失败");
return;
}
// 更新内存中的字段
winbollHost = host;
// 保存到 SP 持久化(私有模式,安全)
SharedPreferences sp = sInstance.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().putString(SP_KEY_WINBOLL_HOST, host).apply();
LogUtils.d(TAG, "setWinbollHost: 服务器地址已设置并持久化host=" + host);
}
// 新增:获取 WinBoLL 服务器主机地址(优先内存,内存为空则从 SP 读取)
public static String getWinbollHost() {
if (winbollHost != null) {
// 内存中存在,直接返回(提高效率)
return winbollHost;
}
if (sInstance == null) {
LogUtils.e(TAG, "getWinbollHost: 应用未初始化,获取失败");
return null;
}
// 内存中不存在,从 SP 读取并更新到内存
SharedPreferences sp = sInstance.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
winbollHost = sp.getString(SP_KEY_WINBOLL_HOST, null);
LogUtils.d(TAG, "getWinbollHost: 从 SP 读取服务器地址host=" + winbollHost);
return winbollHost;
}
/**
* 应用启动时初始化(仅执行一次)
* 初始化核心框架、恢复调试状态、配置全局异常处理等
@@ -85,12 +123,13 @@ public class GlobalApplication extends Application {
super.onCreate();
// 初始化单例实例(确保在所有初始化操作前完成)
sInstance = this;
// 初始化基础组件日志、崩溃处理、Toast
initCoreComponents();
// 恢复/初始化调试模式状态(从本地文件读取,无文件则默认关闭调试)
restoreDebugStatus();
// 新增:初始化服务器地址(从 SP 读取到内存,提高后续访问效率)
initWinbollHost();
LogUtils.d(TAG, "GlobalApplication 初始化完成,单例实例已创建");
}
@@ -115,7 +154,7 @@ public class GlobalApplication extends Application {
private void restoreDebugStatus() {
// 从文件加载 APPModel 实例(存储调试状态的模型类)
APPModel appModel = APPModel.loadBeanFromFile(
getAppModelFilePath(this),
getAppModelFilePath(this),
APPModel.class
);
@@ -131,6 +170,11 @@ public class GlobalApplication extends Application {
}
}
// 新增:初始化服务器地址(应用启动时从 SP 读取到内存)
private void initWinbollHost() {
getWinbollHost(); // 触发从 SP 读取并更新内存
}
/**
* 获取应用名称(从 AndroidManifest.xml 的 android:label 读取)
* @param context 上下文(建议传入 Application 上下文,避免内存泄漏)
@@ -154,7 +198,7 @@ public class GlobalApplication extends Application {
return appName;
} catch (NameNotFoundException e) {
// 包名不存在(理论上不会发生,捕获异常避免崩溃)
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
//LogUtils.e(TAG, "获取应用名称失败:包名不存在", e);
e.printStackTrace();
}
@@ -170,7 +214,6 @@ public class GlobalApplication extends Application {
// 释放单例引用(可选,避免内存泄漏风险)
sInstance = null;
LogUtils.d(TAG, "GlobalApplication 终止,单例实例已释放");
}
}