From 80f2ca2df62ff1cd03046864af504245be078eb2 Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期四, 05 六月 2025 10:37:28 +0800 Subject: [PATCH] 2025年06月05日10:37:26 --- app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java | 158 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 134 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java index 13d7ea1..1d87970 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java @@ -1,15 +1,21 @@ package com.hdl.photovoltaic.other; -import android.app.Dialog; import android.content.Context; +import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Looper; +import android.util.Log; import android.widget.Toast; -import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.content.res.AppCompatResources; +import com.hdl.linkpm.sdk.core.exception.HDLException; +import com.hdl.photovoltaic.R; import com.hdl.photovoltaic.enums.ShowErrorMode; -import com.hdl.photovoltaic.widget.ConfirmationDialog; +import com.hdl.photovoltaic.widget.ConfirmationCancelDialog; +import com.hdl.photovoltaic.widget.ConfirmationExceptionDialog; +import com.hdl.photovoltaic.widget.FlashingBoxDialog; +import com.hdl.photovoltaic.widget.FlashingIconBoxDialog; /** * 绾跨▼閫昏緫 @@ -21,19 +27,37 @@ /** * 鍒囨崲鍥炰富绾跨▼鎵ц * - * @param run 鍥炶皟 + * @param runnable 鍥炶皟 * @param context 涓婁笅鏂�(涓嶉渶瑕佸脊妗嗗~null) * @param showErrorMode 鏄惁鏄剧ず閿欒(涓嶉渶瑕佹樉绀洪敊璇~null) */ - public static void runMainThread(Runnable run, Context context, ShowErrorMode showErrorMode) { + public static void runMainThread(Runnable runnable, Context context, ShowErrorMode showErrorMode) { try { if (Looper.myLooper() == Looper.getMainLooper()) { - run.run(); + runnable.run(); } else { - handler.post(run); + handler.post(runnable); } } catch (Exception e) { + exception(e, showErrorMode, context); + } + } + + /** + * 鍒囨崲鍥炰富绾跨▼鎵ц + * + * @param runnable 鍥炶皟 + */ + public static void runMainThread(Runnable runnable) { + try { + if (Looper.myLooper() == Looper.getMainLooper()) { + runnable.run(); + } else { + handler.post(runnable); + } + } catch (Exception ignored) { + } } @@ -41,19 +65,38 @@ /** * 瀛愮嚎绋嬫墽琛� * - * @param run 鍥炶皟 + * @param runnable 鍥炶皟 * @param context 涓婁笅鏂�(涓嶉渶瑕佸脊妗嗗~null) * @param showErrorMode 鏄惁鏄剧ず閿欒(涓嶉渶瑕佸~null) */ - public static void runThread(Runnable run, Context context, ShowErrorMode showErrorMode) { + public static void runSubThread(Runnable runnable, Context context, ShowErrorMode showErrorMode) { new Thread(new Runnable() { @Override public void run() { try { - run.run(); + runnable.run(); } catch (Exception e) { exception(e, showErrorMode, context); + } + } + }).start(); + + } + + /** + * 瀛愮嚎绋嬫墽琛� + * + * @param runnable 鍥炶皟 + */ + public static void runSubThread(Runnable runnable) { + + new Thread(new Runnable() { + @Override + public void run() { + try { + runnable.run(); + } catch (Exception ignored) { } } }).start(); @@ -68,7 +111,25 @@ handler.post(new Runnable() { @Override public void run() { - Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); + FlashingBoxDialog flashingBoxDialog = new FlashingBoxDialog(context, 1500); + flashingBoxDialog.setContent(text); + flashingBoxDialog.show(); +// Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); + } + }); + } + + /** + * 鍏ㄥ眬寮瑰嚭妗�,绯荤粺榛樿Toast + */ + public static void toast(final Context context, final HDLException hdlException) { + handler.post(new Runnable() { + @Override + public void run() { +// FlashingBoxDialog flashingIconBoxDialog = new FlashingBoxDialog(context, 1500); +// flashingIconBoxDialog.setContent(hdlException.getMsg() + "(" + hdlException.getCode() + ")"); +// flashingIconBoxDialog.show(); + Toast.makeText(context, hdlException.getMsg() + "(" + hdlException.getCode() + ")", Toast.LENGTH_SHORT).show(); } }); } @@ -80,11 +141,57 @@ handler.post(new Runnable() { @Override public void run() { + Toast.makeText(context, resId, Toast.LENGTH_SHORT).show(); } }); } + + /** + * 鍏ㄥ眬寮瑰嚭妗�,閿欒鎻愮ず妗嗭紙钃濇箹涓婃牱寮忥級 + * + * @param isBoolean 鍥炬爣(true浣跨敤鎴愬姛鍥炬爣,false浣跨敤澶辫触鍥炬爣) + * @param msg 淇℃伅 + * @param code 鐘舵�� + */ + public static void tipFlashingBox(Context context, boolean isBoolean, String msg, int code) { + tipFlashingBox(context, isBoolean, msg, code, 1500); + } + + /** + * 鍏ㄥ眬寮瑰嚭妗�,閿欒鎻愮ず妗嗭紙钃濇箹涓婃牱寮忥級 + * + * @param isBoolean 鍥炬爣(true浣跨敤鎴愬姛鍥炬爣,false浣跨敤澶辫触鍥炬爣) + * @param msg 淇℃伅 + * @param code 鐘舵�� + * @param millisecond 璁剧疆寤舵椂鏃堕棿鍏抽棴寮圭獥(鍗曚綅ms) + */ + public static void tipFlashingBox(Context context, boolean isBoolean, String msg, int code, int millisecond) { + if (context == null) { + return; + } + HdlThreadLogic.runMainThread(new Runnable() { + @Override + public void run() { + FlashingIconBoxDialog flashingIconBoxDialog = new FlashingIconBoxDialog(context, millisecond); + if (isBoolean) { + Drawable drawable = AppCompatResources.getDrawable(context, R.drawable.tip_succeed); + flashingIconBoxDialog.setImage(drawable); + } else { + Drawable drawable = AppCompatResources.getDrawable(context, R.drawable.tip_fail); + flashingIconBoxDialog.setImage(drawable); + } + if (code == 0) { + //鎴愬姛涓嶉渶瑕佸鍔犵姸鎬佺爜 + flashingIconBoxDialog.setContent(msg); + } else { + flashingIconBoxDialog.setContent(msg + "(" + code + ")"); + } + flashingIconBoxDialog.show(); + } + }, null, null); + } /** * @param ex 寮傚父淇℃伅绫� @@ -103,24 +210,27 @@ @Override public void run() { - ConfirmationDialog confirmationDialog = new ConfirmationDialog(context); - confirmationDialog.setTitle("鎻愮ず"); - confirmationDialog.setContent("寰堟姳姝�,绋嬪簭鍑虹幇閿欒浜�,鐐瑰嚮\"纭\"鑾峰彇鏇村璇︾粏閿欒淇℃伅."); - confirmationDialog.setConfirmation("纭"); - confirmationDialog.setCancel("鍙栨秷"); - confirmationDialog.show(); - confirmationDialog.setNoOnclickListener(new ConfirmationDialog.onNoOnclickListener() { + ConfirmationCancelDialog confirmationCancelDialog = new ConfirmationCancelDialog(context); + confirmationCancelDialog.setTitle("鎻愮ず"); + confirmationCancelDialog.setContent("寰堟姳姝�,绋嬪簭鍑虹幇閿欒浜�,鐐瑰嚮\"纭\"鑾峰彇鏇村璇︾粏閿欒淇℃伅."); + confirmationCancelDialog.setConfirmation("纭"); + confirmationCancelDialog.setCancel("鍙栨秷"); + confirmationCancelDialog.show(); + confirmationCancelDialog.setNoOnclickListener(new ConfirmationCancelDialog.onNoOnclickListener() { @Override public void Cancel() { - confirmationDialog.dismiss(); + confirmationCancelDialog.dismiss(); } }); - confirmationDialog.setYesOnclickListener(new ConfirmationDialog.onYesOnclickListener() { + confirmationCancelDialog.setYesOnclickListener(new ConfirmationCancelDialog.onYesOnclickListener() { @Override public void Confirm() { - confirmationDialog.dismiss(); - String s = getStackTrace(ex); - + confirmationCancelDialog.dismiss(); + ConfirmationExceptionDialog confirmationExceptionDialog = new ConfirmationExceptionDialog(context); +// String s = getStackTrace(ex); + String s = Log.getStackTraceString(ex); + confirmationExceptionDialog.setContent(s); + confirmationExceptionDialog.show(); } }); @@ -132,7 +242,7 @@ * @return 璋冪敤鏍� */ private static String getStackTrace(Exception ex) { - StringBuilder sb = new StringBuilder(""); + StringBuilder sb = new StringBuilder(); StackTraceElement[] trace = ex.getStackTrace(); for (StackTraceElement stackTraceElement : trace) { sb.append(stackTraceElement).append("\n"); -- Gitblit v1.8.0