Enable/disable vibration function in android?

2020-03-31 09:22发布

I have implemented vibration using vibrator .In my application, when the user press the button, vibration works.For some users wont like vibration in app so i had a toggle button as vibration on/off. i just need to know how to implement the enable/disable the vibration function. this is my vibrator class

      Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);  
     vib.vibrate(500);
      Toast.makeText(this, "vib started", Toast.LENGTH_LONG).show();

1条回答
闹够了就滚
2楼-- · 2020-03-31 09:39

use boolean flag to toggle

if(isVibrate){
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);  
     vib.vibrate(500);
      Toast.makeText(this, "vib started", Toast.LENGTH_LONG).show();
}

else{

// do nothing

}
查看更多
登录 后发表回答