YYPOST群发软件 发表于 2026-4-14 06:35:19

安卓脚本秒吐司快速显示和关闭的UI提示窗口



安卓脚本秒吐司快速显示和关闭的UI提示窗口







/**
* ============================================
* 平台: Android (AIWROK)
* 🍎交流QQ群:711841924(群)
*🍎交流QQ群:528816639(安卓内测群)
* ============================================
*/

// 导入包
importClass(Packages.android.os.Handler);
importClass(Packages.android.os.Looper);

/**
* 秒吐司 - 快速显示和关闭的UI提示窗口(使用floatUI,最上层显示)
* @param {string} message - 提示信息
* @param {number} duration - 显示时长(毫秒),默认3000ms
*/
function showQuickToast(message, duration) {
    duration = duration || 3000;
   
    try {
      // 创建 floatUI 实例
      var fui = new floatUI();
      
      // 加载简单的文本布局(红色大字,蓝色背景)
      fui.loadXML(
            '<TextView xmlns:android="http://schemas.android.com/apk/res/android" ' +
            'android:id="toastText" ' +
            'android:layout_width="wrap_content" ' +
            'android:layout_height="wrap_content" ' +
            'android:text="' + message + '" ' +
            'android:textColor="#FF0000" ' +
            'android:textSize="20sp" ' +
            'android:textStyle="bold" ' +
            'android:background="#EE4A90E2" ' +
            'android:padding="20dp" ' +
            'android:gravity="center" />'
      );
      
      // 获取屏幕尺寸
      var screenWidth = screen.getScreenWidth();
      var screenHeight = screen.getScreenHeight();
      
      // 设置位置(屏幕上方居中)
      setTimeout(function() {
            fui.setPosition((screenWidth - 300) / 2, screenHeight / 3);
            
            // 设置为最上层窗口类型
            try {
                var window = fui.getWindow();
                if (window) {
                  var params = window.getAttributes();
                  // TYPE_APPLICATION_OVERLAY - Android 8.0+
                  // TYPE_SYSTEM_ALERT - Android 8.0以下
                  params.type = android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                  // 或者使用更高的层级
                  // params.type = 2038; // TYPE_APPLICATION_OVERLAY 的值
                  window.setAttributes(params);
                }
            } catch (e) {
                // 如果设置失败,忽略错误
            }
      }, 50);
      
      // 自动关闭
      setTimeout(function() {
            try {
                fui.close();
            } catch (e) {
                // 忽略关闭错误
            }
      }, duration);
      
    } catch (e) {
      printl("❌ 秒吐司显示失败: " + e.message);
      // 降级方案:直接打印
      printl("[提示] " + message);
    }
}

/**
* 在指定位置显示秒吐司(最上层浮动窗)
* @param {string} message - 提示信息
* @param {string} position - 位置:'top'(上), 'bottom'(下), 'left'(左), 'right'(右)
* @param {number} duration - 显示时长(毫秒)
*/
function showQuickToastAt(message, position, duration) {
    duration = duration || 3000;
   
    try {
      var fui = new floatUI();
      
      fui.loadXML(
            '<TextView xmlns:android="http://schemas.android.com/apk/res/android" ' +
            'android:id="toastText" ' +
            'android:layout_width="wrap_content" ' +
            'android:layout_height="wrap_content" ' +
            'android:text="' + message + '" ' +
            'android:textColor="#FF0000" ' +
            'android:textSize="20sp" ' +
            'android:textStyle="bold" ' +
            'android:background="#EE4A90E2" ' +
            'android:padding="20dp" ' +
            'android:gravity="center" />'
      );
      
      var screenWidth = screen.getScreenWidth();
      var screenHeight = screen.getScreenHeight();
      var x, y;
      
      // 根据位置设置坐标
      switch(position) {
            case 'top':
                x = (screenWidth - 300) / 2;
                y = screenHeight / 4;
                break;
            case 'bottom':
                x = (screenWidth - 300) / 2;
                y = screenHeight * 3 / 4;
                break;
            case 'left':
                x = screenWidth / 4;
                y = (screenHeight - 100) / 2;
                break;
            case 'right':
                x = screenWidth * 3 / 4;
                y = (screenHeight - 100) / 2;
                break;
            default:
                x = (screenWidth - 300) / 2;
                y = screenHeight / 3;
      }
      
      setTimeout(function() {
            fui.setPosition(x, y);
            
            // 设置为最上层窗口类型
            try {
                var window = fui.getWindow();
                if (window) {
                  var params = window.getAttributes();
                  params.type = android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                  window.setAttributes(params);
                }
            } catch (e) {
                // 如果设置失败,忽略错误
            }
      }, 50);
      
      setTimeout(function() {
            try {
                fui.close();
            } catch (e) {
                // 忽略关闭错误
            }
      }, duration);
      
    } catch (e) {
      printl("❌ 秒吐司显示失败: " + e.message);
      printl("[提示] " + message);
    }
}



/**
* 测试秒吐司功能
*/
function testQuickToast() {
    printl("=== 测试秒吐司功能 ===");
   
    // 测试1:基本显示
    showQuickToast("这是秒吐司消息");
    sleep.millisecond(1200);
   
    // 测试2:短时间显示
    showQuickToast("短消息", 600);
    sleep.millisecond(1000);
   
    // 测试3:连续显示
    showQuickToast("第一条");
    sleep.millisecond(300);
    showQuickToast("第二条覆盖第一条");
    sleep.millisecond(1200);
   
    printl("=== 测试完成 ===");
}

/**
* 主函数 - 测试秒吐司功能
*/
function main() {
    printl("=== 秒吐司功能演示 ===");
   
    var message = "技术交流QQ群711841924群一,苹果内测群,528816639";
   
    // 演示1:上方显示(3秒)
    printl("1. 上方显示");
    showQuickToastAt(message, 'top', 3000);
    sleep.millisecond(3500);
   
    // 演示2:下方显示(3秒)
    printl("2. 下方显示");
    showQuickToastAt(message, 'bottom', 3000);
    sleep.millisecond(3500);
   
    // 演示3:左侧显示(3秒)
    printl("3. 左侧显示");
    showQuickToastAt(message, 'left', 3000);
    sleep.millisecond(3500);
   
    // 演示4:右侧显示(3秒)
    printl("4. 右侧显示");
    showQuickToastAt(message, 'right', 3000);
    sleep.millisecond(3500);
   
    printl("=== 演示完成 ===");
    printl("\n使用方法:");
    printl("showQuickToast('消息');                  // 默认3秒,上方显示");
    printl("showQuickToastAt('消息', 'top', 3000);   // 上方显示");
    printl("showQuickToastAt('消息', 'bottom', 3000);// 下方显示");
    printl("showQuickToastAt('消息', 'left', 3000);    // 左侧显示");
    printl("showQuickToastAt('消息', 'right', 3000);   // 右侧显示");
}

// 执行主函数
main();




页: [1]
查看完整版本: 安卓脚本秒吐司快速显示和关闭的UI提示窗口