Android Vibrator Usage
package com.example.vibrationdemo; import android.annotation.SuppressLint; import android.app.Service; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.VibrationAttributes; import android.os.VibrationEffect; import android.os.Vibrator; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import com.example.vibrationdemo.R; /** * */ public class MainActivity extends AppCompatActivity { private Button btn_hasVibrator; private Button btn_short; private Button btn_long; private Button btn_rhythm; private Button btn_cancle; private Vibrator myVibrator; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得系统的Vibrator实例: myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); mContext = MainActivity.this; bindViews(); } private void bindViews() { btn_hasVibrator = (Button) findViewById(R.id.btn_hasVibrator); btn_short = (Button) findViewById(R.id.btn_short); btn_long = (Button) findViewById(R.id.btn_long); btn_rhythm = (Button) findViewById(R.id.btn_rhythm); btn_cancle = (Button) findViewById(R.id.btn_cancle); btn_hasVibrator.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, myVibrator.hasVibrator() ? "当前设备有振动器" : "当前设备无振动器", Toast.LENGTH_SHORT).show(); } }); btn_short.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.Q) @Override public void onClick(View v) { myVibrator.cancel(); // myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0); // myVibrator.vibrate(VibrationEffect.createWaveform(new long[]{100, 200, 100, 200}, 0)); // repeatedly myVibrator.vibrate(VibrationEffect.createWaveform(new long[]{100, 200, 100, 200}, -1)); // only once // myVibrator.vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE)); // myVibrator.vibrate(VibrationEffect.createOneShot(10, VibrationEffect.EFFECT_CLICK)); // myVibrator.vibrate(VibrationEffect.createOneShot(10, VibrationEffect.EFFECT_DOUBLE_CLICK)); // myVibrator.vibrate(VibrationEffect.createOneShot(10, VibrationEffect.EFFECT_HEAVY_CLICK)); // myVibrator.vibrate(VibrationEffect.createOneShot(10, VibrationEffect.EFFECT_TICK)); Toast.makeText(mContext, "短振动", Toast.LENGTH_SHORT).show(); } }); btn_long.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myVibrator.cancel(); // myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0); myVibrator.vibrate(VibrationEffect.createWaveform(new long[]{100, 100, 100, 1000}, 0)); Toast.makeText(mContext, "长振动", Toast.LENGTH_SHORT).show(); } }); btn_rhythm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myVibrator.cancel(); // myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0); myVibrator.vibrate(VibrationEffect.createWaveform(new long[]{500, 100, 500, 100, 500, 100}, 0)); Toast.makeText(mContext, "节奏振动", Toast.LENGTH_SHORT).show(); } }); btn_cancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myVibrator.cancel(); Toast.makeText(mContext, "取消振动", Toast.LENGTH_SHORT).show(); } }); } }
AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE"/>
说明:android Q(8.0)/API26 以后vibrator接口更新为:
Vibrator.vibrate(arg1, arg2); =>
Vibrator.vibrate(VibratorEffect)
refs:
https://developer.android.google.cn/reference/android/os/Vibrator#vibrate(android.os.VibrationEffect,%20android.os.VibrationAttributes)
https://www.runoob.com/w3cnote/android-tutorial-powermanager.html