跳到内容
CrossFly 是 100% 开源在 GitHub 关注我们
CrossFlyCrossFly
开始使用

本页面由葡萄牙语机器翻译,可能存在错误。葡萄牙语版本为准。

设备声音与实用功能

门面 Device 汇集了小巧且可移植的功能。全部为静态方法。

反馈与系统

Device.beep();              // 系统提示音
Device.vibrate(200);        // 震动 200 毫秒
Device.sleep(500);          // 暂停当前线程 500 毫秒
int livre = Device.getFreeMemory();
Device.show("https://exemplo.com");   // 在操作系统的外部应用中打开 URL/资源

剪贴板

Device.clipboardCopy("texto");
String colado = Device.clipboardPaste();

身份与平台

if (Device.platform.isMobile()) { ... }     // ANDROID/IPHONE/IPAD
if (Device.platform.isDesktop()) { ... }    // WINDOWS/LINUX/MACOS
if (Device.platform.isIOS()) { ... }        // IPHONE/IPAD
String base = Device.homeFolder;            // 应用的基础目录

Platform:JAVA · WINDOWS · LINUX · IPHONE · ANDROID · IPAD · MACOS。

无线电

WirelessState wifi = Device.getWirelessState(WirelessType.WIFI);   // UNSUPPORTED/DISABLED/ENABLED
Device.enableWireless(WirelessType.BLUETOOTH, true);

WirelessType:WIFI · PHONE · BLUETOOTH。

语音 —— TTS 与 STT

Device.textToVoice("Olá, mundo");   // 朗读文本 (TTS)

Device.voiceToText(new Device.VoiceToTextListener() {   // 语音识别 (STT)
   public void onStart() {}
   public void onFinish() {}
   public void onResult(String texto) { Component.onUI(() -> campo.text(texto)); }
   public void onError(int code, String msg) { Component.onUI(() -> Hint.show(msg, 2000)); }
});

STT 的回调会在 绘制线程之外 到达 → 通过 Component.onUI(...) 返回。

⚠️ 迁移说明

  • Sound.* 和 RadioState 已被 Device 吸收 —— Sound.tone/play 已不复存在。
  • 在 Web/TeaVM 上,其中大部分为 stub(震动、无线电、语音)—— 请处理 Web 目标。

最佳实践

  • 语音/无线电回调通过 onUI(...) 返回 UI。
  • 在假定某个无线电存在之前,先检查 WirelessState.UNSUPPORTED。

常见错误

  • 调用 Sound.play(...)/RadioState(已移除)—— 请使用 Device。
  • 在 STT 的 onResult 中未经 onUI 直接操作 UI。

另请参阅

这个页面有帮助吗?