小程序初始提交
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
|
||||
package="io.dcloud.uni_modules.xVibrateS">
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
</manifest>
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"minSdkVersion": "19",
|
||||
"targetSdkVersion": "34"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import Vibrator from "android.os.Vibrator";
|
||||
import VibratorManager from "android.os.VibratorManager";
|
||||
import VibrationEffect from "android.os.VibrationEffect";
|
||||
import Context from "android.content.Context";
|
||||
import Build from "android.os.Build";
|
||||
|
||||
/**
|
||||
* 震动
|
||||
* @param {number} duration 震动时间单位ms
|
||||
*/
|
||||
export function vibrate(duration: number) {
|
||||
try {
|
||||
const context = UTSAndroid.getAppContext() as Context;
|
||||
let vb: Vibrator | null = null;
|
||||
|
||||
// Android 12 (API 31) 及以上使用 VibratorManager
|
||||
if (Build.VERSION.SDK_INT >= 31) {
|
||||
const vibratorManager = context.getSystemService(
|
||||
Context.VIBRATOR_MANAGER_SERVICE
|
||||
) as VibratorManager;
|
||||
vb = vibratorManager.getDefaultVibrator();
|
||||
}
|
||||
|
||||
if (vb != null && vb.hasVibrator()) {
|
||||
// Android 8.0 (API 26) 及以上使用 VibrationEffect
|
||||
if (Build.VERSION.SDK_INT >= 26) {
|
||||
const effect = VibrationEffect.createOneShot(
|
||||
duration.toLong(),
|
||||
VibrationEffect.DEFAULT_AMPLITUDE
|
||||
);
|
||||
vb.vibrate(effect);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("震动失败:", e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user