W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
當設備需要設置不同的振動效果時,可以調用 Vibrator 模塊,例如,設備的按鍵可以設置不同強度和時長的振動,鬧鐘和來電可以設置不同強度和時長的單次或周期性振動。
振動器模塊主要提供的功能有:查詢設備上振動器的列表,查詢某個振動器是否支持某種振動效果,觸發(fā)和關閉振動器。VibratorAgent 類開放能力如下,具體請查閱 API 參考文檔。
接口名 | 描述 |
---|---|
getVibratorIdList() | 獲取硬件設備上的振動器列表。 |
isSupport(int) | 根據指定的振動器Id查詢硬件設備是否存在該振動器。 |
isEffectSupport(int, String) | 查詢指定的振動器是否支持指定的震動效果。 |
startOnce(int, String) | 對指定的振動器創(chuàng)建指定效果的一次性振動。 |
startOnce(String) | 對指定的振動器創(chuàng)建指定效果的一次性振動。 |
startOnce(int, int) | 對指定的振動器創(chuàng)建指定振動時長的一次性振動。 |
startOnce(int) | 對指定的振動器創(chuàng)建指定振動時長的一次性振動。 |
start(int, VibrationPattern) | 對指定的振動器創(chuàng)建自定義效果的波形或一次性振動。 |
start(VibrationPattern) | 對指定的振動器創(chuàng)建自定義效果的波形或一次性振動。 |
stop(int, String) | 關閉指定的振動器指定模式的振動。 |
stop(String) | 關閉指定的振動器指定模式的振動。 |
"reqPermissions": [
{
"name": "ohos.permission.VIBRATE",
"reason": "",
"usedScene": {
"ability": [
".MainAbility"
],
"when": "inuse"
}
}
]
private VibratorAgent vibratorAgent = new VibratorAgent();
private int[] timing = {1000, 1000, 2000, 5000};
private int[] intensity = {50, 100, 200, 255};
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_vibrator_layout);
// ...
// 查詢硬件設備上的振動器列表
List<Integer> vibratorList = vibratorAgent.getVibratorIdList();
if (vibratorList.isEmpty()) {
return;
}
int vibratorId = vibratorList.get(0);
// 查詢指定的振動器是否支持指定的振動效果
boolean isSupport = vibratorAgent.isEffectSupport(vibratorId,
VibrationPattern.VIBRATOR_TPYE_CAMERA_CLICK);
// 創(chuàng)建指定效果的一次性振動
boolean vibrateEffectResult = vibratorAgent.vibrate(vibratorId,
VibrationPattern.VIBRATOR_TPYE_CAMERA_CLICK);
// 創(chuàng)建指定振動時長的一次性振動
int vibratorTiming = 1000;
boolean vibrateResult = vibratorAgent.vibrate(vibratorId, vibratorTiming);
// 創(chuàng)建自定義效果的周期性波形振動
int count = 5;
VibrationPattern vibrationPeriodEffect = VibrationPattern.createPeriod(timing, intensity, count);
boolean vibratePeriodResult = vibratorAgent.vibrate(vibratorId, vibrationPeriodEffect);
// 創(chuàng)建自定義效果的一次性振動
VibrationPattern vibrationOnceEffect = VibrationPattern.createSingle(3000, 50);
boolean vibrateSingleResult = vibratorAgent.vibrate(vibratorId, vibrationOnceEffect);
// 關閉指定的振動器自定義模式的振動
boolean stopResult = vibratorAgent.stop(vibratorId,
VibratorAgent.VIBRATOR_STOP_MODE_CUSTOMIZED);
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: