新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OSVibrator開發(fā)指導(dǎo)
場景介紹

十余年品牌的成都網(wǎng)站建設(shè)公司,超過千家企業(yè)網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn).價(jià)格合理,可準(zhǔn)確把握網(wǎng)頁設(shè)計(jì)訴求.提供定制網(wǎng)站建設(shè)、購物商城網(wǎng)站建設(shè)、微信小程序、響應(yīng)式網(wǎng)站開發(fā)等服務(wù),我們?cè)O(shè)計(jì)的作品屢獲殊榮,是您值得信賴的專業(yè)網(wǎng)站設(shè)計(jì)公司。
當(dāng)設(shè)備需要設(shè)置不同的振動(dòng)效果時(shí),可以調(diào)用 Vibrator 模塊,例如,設(shè)備的按鍵可以設(shè)置不同強(qiáng)度和時(shí)長的振動(dòng),鬧鐘和來電可以設(shè)置不同強(qiáng)度和時(shí)長的單次或周期性振動(dòng)。
接口說明
振動(dòng)器模塊主要提供的功能有:查詢?cè)O(shè)備上振動(dòng)器的列表,查詢某個(gè)振動(dòng)器是否支持某種振動(dòng)效果,觸發(fā)和關(guān)閉振動(dòng)器。VibratorAgent 類開放能力如下,具體請(qǐng)查閱 API 參考文檔。
| 接口名 | 描述 |
|---|---|
| getVibratorIdList() | 獲取硬件設(shè)備上的振動(dòng)器列表。 |
| isSupport(int) | 根據(jù)指定的振動(dòng)器Id查詢硬件設(shè)備是否存在該振動(dòng)器。 |
| isEffectSupport(int, String) | 查詢指定的振動(dòng)器是否支持指定的震動(dòng)效果。 |
| startOnce(int, String) | 對(duì)指定的振動(dòng)器創(chuàng)建指定效果的一次性振動(dòng)。 |
| startOnce(String) | 對(duì)指定的振動(dòng)器創(chuàng)建指定效果的一次性振動(dòng)。 |
| startOnce(int, int) | 對(duì)指定的振動(dòng)器創(chuàng)建指定振動(dòng)時(shí)長的一次性振動(dòng)。 |
| startOnce(int) | 對(duì)指定的振動(dòng)器創(chuàng)建指定振動(dòng)時(shí)長的一次性振動(dòng)。 |
| start(int, VibrationPattern) | 對(duì)指定的振動(dòng)器創(chuàng)建自定義效果的波形或一次性振動(dòng)。 |
| start(VibrationPattern) | 對(duì)指定的振動(dòng)器創(chuàng)建自定義效果的波形或一次性振動(dòng)。 |
| stop(int, String) | 關(guān)閉指定的振動(dòng)器指定模式的振動(dòng)。 |
| stop(String) | 關(guān)閉指定的振動(dòng)器指定模式的振動(dòng)。 |
開發(fā)步驟
- 控制設(shè)備上的振動(dòng)器,需要在“config.json”里面進(jìn)行配置請(qǐng)求權(quán)限。具體如下:
"reqPermissions": [
{
"name": "ohos.permission.VIBRATE",
"reason": "",
"usedScene": {
"ability": [
".MainAbility"
],
"when": "inuse"
}
}
]
- 查詢硬件設(shè)備上的振動(dòng)器列表。
- 查詢指定的振動(dòng)器是否支持指定的震動(dòng)效果。
- 創(chuàng)建不同效果的振動(dòng)。
- 關(guān)閉指定的振動(dòng)器指定模式的振動(dòng)。
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);
// ...
// 查詢硬件設(shè)備上的振動(dòng)器列表
List vibratorList = vibratorAgent.getVibratorIdList();
if (vibratorList.isEmpty()) {
return;
}
int vibratorId = vibratorList.get(0);
// 查詢指定的振動(dòng)器是否支持指定的振動(dòng)效果
boolean isSupport = vibratorAgent.isEffectSupport(vibratorId,
VibrationPattern.VIBRATOR_TPYE_CAMERA_CLICK);
// 創(chuàng)建指定效果的一次性振動(dòng)
boolean vibrateEffectResult = vibratorAgent.vibrate(vibratorId,
VibrationPattern.VIBRATOR_TPYE_CAMERA_CLICK);
// 創(chuàng)建指定振動(dòng)時(shí)長的一次性振動(dòng)
int vibratorTiming = 1000;
boolean vibrateResult = vibratorAgent.vibrate(vibratorId, vibratorTiming);
// 創(chuàng)建自定義效果的周期性波形振動(dòng)
int count = 5;
VibrationPattern vibrationPeriodEffect = VibrationPattern.createPeriod(timing, intensity, count);
boolean vibratePeriodResult = vibratorAgent.vibrate(vibratorId, vibrationPeriodEffect);
// 創(chuàng)建自定義效果的一次性振動(dòng)
VibrationPattern vibrationOnceEffect = VibrationPattern.createSingle(3000, 50);
boolean vibrateSingleResult = vibratorAgent.vibrate(vibratorId, vibrationOnceEffect);
// 關(guān)閉指定的振動(dòng)器自定義模式的振動(dòng)
boolean stopResult = vibratorAgent.stop(vibratorId,
VibratorAgent.VIBRATOR_STOP_MODE_CUSTOMIZED);
} 分享文章:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OSVibrator開發(fā)指導(dǎo)
文章轉(zhuǎn)載:http://fisionsoft.com.cn/article/djiooce.html


咨詢
建站咨詢
