Nokia UI API 概覽
Nokia 在標準 J2ME (MIDP/CLDC) 基礎上擴充了專屬 API,統稱 Nokia UI API,封裝於 com.nokia.mid.ui 套件。其中 DeviceControl 類別提供背景燈閃爍、亮度調節、手機震動三大硬體控制功能,是打造沉浸式遊戲體驗的關鍵工具。
開發環境配置
- 安裝 Nokia Developer's Suite 或對應 SDK。
- 專案引用 SDK 目錄下的
nokia-ui.jar(通常位於 Devices/<模擬器>/lib/)。
- 原始碼匯入:
import com.nokia.mid.ui.DeviceControl;
核心方法詳解
1. FlashLights(long duration) — 背景燈閃爍
public static void FlashLights(long duration)
| 參數 | 說明 |
| duration | 閃爍持續時間(毫秒),< 0 拋出 IllegalArgumentException |
- 非阻塞:呼叫後立即返回,背景執行緒處理閃爍。
- 自動復原:閃爍結束自動恢復原亮度狀態。
- 提前結束:呼叫
FlashLights(0) 可立即停止閃爍。
- 相容性:不支援裝置會靜默忽略,不拋出例外。
// 來電/訊息提醒:背景燈閃爍 3 秒
DeviceControl.FlashLights(3000);
// 遊戲關鍵時刻:閃爍 500ms 強調
DeviceControl.FlashLights(500);
2. setLights(int num, int level) — 背景燈亮度控制
public static void setLights(int num, int level)
| 參數 | 說明 |
| num | 燈組編號,0 為主螢幕背光,其它保留 |
| level | 亮度 0-100,0 關閉,100 最大亮度 |
// 省電模式:降低至 30%
DeviceControl.setLights(0, 30);
// 戶外模式:最大亮度
DeviceControl.setLights(0, 100);
// 關閉背光(如待機畫面)
DeviceControl.setLights(0, 0);
3. startVibra(int freq, long duration) — 震動啟動
public static void startVibra(int freq, long duration)
| 參數 | 說明 |
| freq | 頻率 1-100,100 為最強震動 |
| duration | 持續時間(毫秒) |
- 非阻塞:立即返回,震動於背景執行。
- 適用場景:碰撞回饋、得分慶祝、來電震動模擬。
// 輕微震動 200ms(按鍵回饋)
DeviceControl.startVibra(30, 200);
// 強烈震動 1 秒(遊戲失敗/爆炸)
DeviceControl.startVibra(80, 1000);
4. stopVibra() — 震動停止
public static void stopVibra()
- 立即停止進行中的震動,若無震動則靜默返回。
- 配合
MIDlet.pauseApp() 使用,防止應用切換至背景時持續震動消耗電量。
完整實戰範例:遊戲回饋系統
public class GameFeedback {
private static boolean vibraEnabled = true;
private static boolean lightsEnabled = true;
public static void onButtonPress() {
if (vibraEnabled) DeviceControl.startVibra(20, 50);
if (lightsEnabled) DeviceControl.FlashLights(100);
}
public static void onExplosion() {
if (vibraEnabled) DeviceControl.startVibra(90, 800);
if (lightsEnabled) DeviceControl.FlashLights(500);
}
public static void onPause() {
DeviceControl.stopVibra(); // 確保不在背景震動
}
public static void setVibraEnabled(boolean enabled) {
vibraEnabled = enabled;
if (!enabled) DeviceControl.stopVibra();
}
}
跨廠商相容性建議
其他廠商(Sony Ericsson, Motorola, Samsung)亦有類似擴充 API,但類別/方法命名不同。建議採用介面隔離模式:
public interface IDeviceFeedback {
void vibrate(int intensity, long duration);
void flashLight(long duration);
void setBacklight(int level);
}
// Nokia 實作
public class NokiaFeedback implements IDeviceFeedback { ... }
// 其他廠商實作...
public class SiemensFeedback implements IDeviceFeedback { ... }
// 執行期依裝置載入對應實作
String vendor = System.getProperty("microedition.platform");
IDeviceFeedback feedback = DeviceFeedbackFactory.create(vendor);