Search code examples
androidservicevibrationandroid-vibration

Enable/disable vibration function in android?


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();

Solution

  • 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
    
    }