This commit is contained in:
844143714@qq,com 2026-05-16 01:39:18 +08:00
parent 119252cb4b
commit b4f61c4b75
4 changed files with 33 additions and 35 deletions

View File

@ -287,12 +287,12 @@ class AndroidInterface(private val activity: MainActivity) {
val zipPath = LogManager.zipLogs(activity) val zipPath = LogManager.zipLogs(activity)
if (zipPath != null) { if (zipPath != null) {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
Toast.makeText(activity, "日志已打包: $zipPath", Toast.LENGTH_LONG).show() Toast.makeText(activity.applicationContext, "日志已打包: $zipPath", Toast.LENGTH_LONG).show()
} }
return zipPath return zipPath
} else { } else {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
Toast.makeText(activity, "暂无日志或打包失败", Toast.LENGTH_SHORT).show() Toast.makeText(activity.applicationContext, "暂无日志或打包失败", Toast.LENGTH_SHORT).show()
} }
return "" return ""
} }
@ -301,7 +301,7 @@ class AndroidInterface(private val activity: MainActivity) {
fun uploadLogs() { fun uploadLogs() {
LogManager.uploadLogs(activity) { success, message -> LogManager.uploadLogs(activity) { success, message ->
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
Toast.makeText(activity, if (success) "日志上传成功" else "上传失败: $message", Toast.LENGTH_SHORT).show() Toast.makeText(activity.applicationContext, if (success) "日志上传成功" else "上传失败: $message", Toast.LENGTH_SHORT).show()
} }
} }
} }
@ -312,7 +312,7 @@ class AndroidInterface(private val activity: MainActivity) {
*/ */
fun printPage() { fun printPage() {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
Toast.makeText(activity, "GeckoView 暂不支持系统打印,请使用热敏打印功能", Toast.LENGTH_SHORT).show() Toast.makeText(activity.applicationContext, "GeckoView 暂不支持系统打印,请使用热敏打印功能", Toast.LENGTH_SHORT).show()
} }
} }
@ -322,7 +322,7 @@ class AndroidInterface(private val activity: MainActivity) {
Timber.tag("Print").d("收到 H5 原始打印数据: $data") Timber.tag("Print").d("收到 H5 原始打印数据: $data")
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
try { try {
Toast.makeText(activity, "H5指令打印: $data", Toast.LENGTH_SHORT).show() Toast.makeText(activity.applicationContext, "H5指令打印: $data", Toast.LENGTH_SHORT).show()
} catch (e: Exception) { } catch (e: Exception) {
Timber.tag("Print").e(e, "Toast显示失败") Timber.tag("Print").e(e, "Toast显示失败")
} }
@ -413,7 +413,7 @@ class AndroidInterface(private val activity: MainActivity) {
try { try {
activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE).edit().clear().apply() activity.getSharedPreferences("msg_records", Context.MODE_PRIVATE).edit().clear().apply()
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
Toast.makeText(activity, "记录已清空", Toast.LENGTH_SHORT).show() Toast.makeText(activity.applicationContext, "记录已清空", Toast.LENGTH_SHORT).show()
} }
} catch (e: Exception) { } catch (e: Exception) {
Timber.tag("Storage").e(e, "清空记录异常") Timber.tag("Storage").e(e, "清空记录异常")

View File

@ -56,7 +56,7 @@ class LoginActivity : AppCompatActivity() {
val password = etPassword.text.toString().trim() val password = etPassword.text.toString().trim()
if (username.isEmpty() || password.isEmpty()) { if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "请输入账号和密码", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "请输入账号和密码", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
@ -70,7 +70,7 @@ class LoginActivity : AppCompatActivity() {
val password = etPassword.text.toString().trim() val password = etPassword.text.toString().trim()
if (username.isEmpty() || password.isEmpty()) { if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "请输入账号和密码", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "请输入账号和密码", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
@ -97,10 +97,10 @@ class LoginActivity : AppCompatActivity() {
if (clickCount >= 10) { if (clickCount >= 10) {
clickCount = 0 clickCount = 0
Toast.makeText(this, "开发者模式已激活", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "开发者模式已激活", Toast.LENGTH_SHORT).show()
startActivity(Intent(this, DeveloperActivity::class.java)) startActivity(Intent(this, DeveloperActivity::class.java))
} else if (clickCount > 5) { } else if (clickCount > 5) {
Toast.makeText(this, "再点击 ${10 - clickCount} 次进入调试模式", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "再点击 ${10 - clickCount} 次进入调试模式", Toast.LENGTH_SHORT).show()
} }
} }
} }
@ -130,7 +130,7 @@ class LoginActivity : AppCompatActivity() {
override fun onFailure(call: Call, e: IOException) { override fun onFailure(call: Call, e: IOException) {
runOnUiThread { runOnUiThread {
loadingDialog.dismiss() loadingDialog.dismiss()
Toast.makeText(this@LoginActivity, "网络异常: ${e.message}", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "网络异常: ${e.message}", Toast.LENGTH_SHORT).show()
} }
} }
@ -156,7 +156,7 @@ class LoginActivity : AppCompatActivity() {
val loginData = data?.toString() ?: "{}" val loginData = data?.toString() ?: "{}"
val accessToken = data?.optString("access_token", "") ?: "" val accessToken = data?.optString("access_token", "") ?: ""
Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "登录成功", Toast.LENGTH_SHORT).show()
// 获取独立的 PTT 账号并初始化 // 获取独立的 PTT 账号并初始化
initPttWithAccount(username, accessToken) initPttWithAccount(username, accessToken)
@ -183,12 +183,12 @@ class LoginActivity : AppCompatActivity() {
finish() finish()
} else { } else {
val msg = json.optString("msg", "登录失败") val msg = json.optString("msg", "登录失败")
Toast.makeText(this@LoginActivity, msg, Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
} }
} catch (e: Exception) { } catch (e: Exception) {
Timber.tag("Login").e(e, "解析响应失败") Timber.tag("Login").e(e, "解析响应失败")
Toast.makeText(this@LoginActivity, "服务器响应异常", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "服务器响应异常", Toast.LENGTH_SHORT).show()
} }
} }
} }
@ -212,7 +212,7 @@ class LoginActivity : AppCompatActivity() {
btnReset.setOnClickListener { btnReset.setOnClickListener {
etServerUrl.setText(AppConfig.DEFAULT_SERVER_URL) etServerUrl.setText(AppConfig.DEFAULT_SERVER_URL)
etSerialPort.setText(AppConfig.DEFAULT_SERIAL_PORT) etSerialPort.setText(AppConfig.DEFAULT_SERIAL_PORT)
Toast.makeText(this, "已重置数值 (待保存)", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "已重置数值 (待保存)", Toast.LENGTH_SHORT).show()
} }
dialogView.findViewById<TextView>(R.id.btn_cancel).setOnClickListener { dialogView.findViewById<TextView>(R.id.btn_cancel).setOnClickListener {
@ -231,7 +231,7 @@ class LoginActivity : AppCompatActivity() {
AppConfig.setServerUrl(this, serverUrl) AppConfig.setServerUrl(this, serverUrl)
AppConfig.setSerialPort(this, serialPort) AppConfig.setSerialPort(this, serialPort)
Toast.makeText(this, "参数已生效", Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, "参数已生效", Toast.LENGTH_SHORT).show()
dialog.dismiss() dialog.dismiss()
} }
@ -351,10 +351,8 @@ class LoginActivity : AppCompatActivity() {
} }
private fun doPttInit(pttUsername: String, pttPassword: String) { private fun doPttInit(pttUsername: String, pttPassword: String) {
runOnUiThread { android.os.Handler(android.os.Looper.getMainLooper()).post {
if (!isFinishing) { Toast.makeText(applicationContext, "PTT初始化中...", Toast.LENGTH_SHORT).show()
Toast.makeText(this@LoginActivity, "PTT初始化中...", Toast.LENGTH_SHORT).show()
}
} }
// 1. PTT 引擎强制重置后登录 (销毁旧 session确保干净状态) // 1. PTT 引擎强制重置后登录 (销毁旧 session确保干净状态)

View File

@ -70,7 +70,7 @@ class UpdateManager(private val context: Context) {
try { try {
val body = response.body?.string() ?: throw IOException("返回内容为空") val body = response.body?.string() ?: throw IOException("返回内容为空")
if (!response.isSuccessful) { if (!response.isSuccessful) {
handler.post { Toast.makeText(context, "更新检查失败: ${response.code}", Toast.LENGTH_SHORT).show() } handler.post { Toast.makeText(context.applicationContext, "更新检查失败: ${response.code}", Toast.LENGTH_SHORT).show() }
return return
} }
@ -89,23 +89,23 @@ class UpdateManager(private val context: Context) {
try { try {
showUpdateDialog(versionName, content, id) showUpdateDialog(versionName, content, id)
} catch (e: Exception) { } catch (e: Exception) {
Toast.makeText(context, "显示更新弹窗失败", Toast.LENGTH_SHORT).show() Toast.makeText(context.applicationContext, "显示更新弹窗失败", Toast.LENGTH_SHORT).show()
} }
} }
} }
} else { } else {
handler.post { Toast.makeText(context, "当前已是最新版本", Toast.LENGTH_SHORT).show() } handler.post { Toast.makeText(context.applicationContext, "当前已是最新版本", Toast.LENGTH_SHORT).show() }
} }
} else { } else {
handler.post { Toast.makeText(context, "当前已是最新版本", Toast.LENGTH_SHORT).show() } handler.post { Toast.makeText(context.applicationContext, "当前已是最新版本", Toast.LENGTH_SHORT).show() }
} }
} else { } else {
val msg = json.optString("msg", "未知错误") val msg = json.optString("msg", "未知错误")
handler.post { Toast.makeText(context, "更新服务提示: $msg", Toast.LENGTH_SHORT).show() } handler.post { Toast.makeText(context.applicationContext, "更新服务提示: $msg", Toast.LENGTH_SHORT).show() }
} }
} catch (e: Exception) { } catch (e: Exception) {
Timber.tag("UpdateManager").e(e, "解析或处理更新结果失败") Timber.tag("UpdateManager").e(e, "解析或处理更新结果失败")
handler.post { Toast.makeText(context, "更新信息解析失败", Toast.LENGTH_SHORT).show() } handler.post { Toast.makeText(context.applicationContext, "更新信息解析失败", Toast.LENGTH_SHORT).show() }
} }
} }
}) })
@ -150,7 +150,7 @@ class UpdateManager(private val context: Context) {
override fun onFailure(call: Call, e: IOException) { override fun onFailure(call: Call, e: IOException) {
handler.post { handler.post {
progressDialog?.dismiss() progressDialog?.dismiss()
Toast.makeText(context, "连接服务器失败", Toast.LENGTH_SHORT).show() Toast.makeText(context.applicationContext, "连接服务器失败", Toast.LENGTH_SHORT).show()
} }
} }
@ -158,7 +158,7 @@ class UpdateManager(private val context: Context) {
if (!response.isSuccessful) { if (!response.isSuccessful) {
handler.post { handler.post {
progressDialog?.dismiss() progressDialog?.dismiss()
Toast.makeText(context, "下载失败: ${response.code}", Toast.LENGTH_SHORT).show() Toast.makeText(context.applicationContext, "下载失败: ${response.code}", Toast.LENGTH_SHORT).show()
} }
return return
} }
@ -169,7 +169,7 @@ class UpdateManager(private val context: Context) {
} catch (e: Exception) { } catch (e: Exception) {
handler.post { handler.post {
progressDialog?.dismiss() progressDialog?.dismiss()
Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show() Toast.makeText(context.applicationContext, "保存失败", Toast.LENGTH_SHORT).show()
} }
} }
} }
@ -210,13 +210,13 @@ class UpdateManager(private val context: Context) {
handler.post { handler.post {
progressDialog?.dismiss() progressDialog?.dismiss()
Toast.makeText(context, "下载完成,文件已保存到系统下载目录", Toast.LENGTH_SHORT).show() Toast.makeText(context.applicationContext, "下载完成,文件已保存到系统下载目录", Toast.LENGTH_SHORT).show()
installApk(targetUri) installApk(targetUri)
} }
} catch (e: Exception) { } catch (e: Exception) {
handler.post { handler.post {
progressDialog?.dismiss() progressDialog?.dismiss()
Toast.makeText(context, "保存失败: ${e.message}", Toast.LENGTH_LONG).show() Toast.makeText(context.applicationContext, "保存失败: ${e.message}", Toast.LENGTH_LONG).show()
} }
} finally { } finally {
inputStream?.close() inputStream?.close()
@ -246,7 +246,7 @@ class UpdateManager(private val context: Context) {
val intent = Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES) val intent = Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
intent.data = Uri.parse("package:" + context.packageName) intent.data = Uri.parse("package:" + context.packageName)
context.startActivity(intent) context.startActivity(intent)
Toast.makeText(context, "请先授予安装权限", Toast.LENGTH_SHORT).show() Toast.makeText(context.applicationContext, "请先授予安装权限", Toast.LENGTH_SHORT).show()
return return
} }
} }
@ -267,7 +267,7 @@ class UpdateManager(private val context: Context) {
context.startActivity(intent) context.startActivity(intent)
} catch (e: Exception) { } catch (e: Exception) {
Timber.tag("UpdateManager").e(e, "安装失败") Timber.tag("UpdateManager").e(e, "安装失败")
Toast.makeText(context, "无法自动调起安装,请在系统下载目录中手动安装", Toast.LENGTH_LONG).show() Toast.makeText(context.applicationContext, "无法自动调起安装,请在系统下载目录中手动安装", Toast.LENGTH_LONG).show()
} }
} }
} }

View File

@ -26,9 +26,9 @@ object PrinterManager {
// 在 Android 系统层面主动提示 // 在 Android 系统层面主动提示
mContext?.let { mContext?.let {
if (!hasPaper) { if (!hasPaper) {
android.widget.Toast.makeText(it, "⚠️ 警告:打印机缺纸!", android.widget.Toast.LENGTH_LONG).show() android.widget.Toast.makeText(it.applicationContext, "⚠️ 警告:打印机缺纸!", android.widget.Toast.LENGTH_LONG).show()
} else { } else {
android.widget.Toast.makeText(it, "打印机状态:正常 (有纸)", android.widget.Toast.LENGTH_SHORT).show() android.widget.Toast.makeText(it.applicationContext, "打印机状态:正常 (有纸)", android.widget.Toast.LENGTH_SHORT).show()
} }
} }