Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Vibrate & VibrationEffect class demo Usage
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
package com.example.nileshdeokar.simplevibratedemo;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
/*
~ Nilesh Deokar @nieldeokar on 03/06/18 11:22 PM
*/
/*
* To test this :
* 1. Paste this activity inside your project.
* 2. Paste `activity_simple_vibrate_demo.xml` into resources folder.
* 3. Add entry of SimpleVibrateDemoActivity inside AndroidManifest.xml
* 4. Add Vibrate permission inside AndroidManifest.xml
*
* <uses-permission android:name="android.permission.VIBRATE" />
*
* 5. Build & run.
* */
public class SimpleVibrateDemoActivity extends AppCompatActivity {
Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_vibrate_demo);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
if (vibrator != null && vibrator.hasVibrator()) {
vibrateFor500ms();
customVibratePatternNoRepeat();
customVibratePatternRepeatFromSpecificIndex();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createOneShotVibrationUsingVibrationEffect();
createWaveFormVibrationUsingVibrationEffect();
createWaveFormVibrationUsingVibrationEffectAndAmplitude();
}
} else {
Toast.makeText(this, "Device does not support vibration", Toast.LENGTH_SHORT).show();
}
}
private void vibrateFor500ms() {
vibrator.vibrate(500);
}
private void customVibratePatternNoRepeat() {
// 0 : Start without a delay
// 400 : Vibrate for 400 milliseconds
// 200 : Pause for 200 milliseconds
// 400 : Vibrate for 400 milliseconds
long[] mVibratePattern = new long[]{0, 400, 200, 400};
// -1 : Do not repeat this pattern
// pass 0 if you want to repeat this pattern from 0th index
vibrator.vibrate(mVibratePattern, -1);
}
private void customVibratePatternRepeatFromSpecificIndex() {
long[] mVibratePattern = new long[]{0, 400, 800, 600, 800, 800, 800, 1000};
// 3 : Repeat this pattern from 3rd element of an array
vibrator.vibrate(mVibratePattern, 3);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createOneShotVibrationUsingVibrationEffect() {
// 1000 : Vibrate for 1 sec
// VibrationEffect.DEFAULT_AMPLITUDE - would perform vibration at full strength
VibrationEffect effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(effect);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createWaveFormVibrationUsingVibrationEffect() {
long[] mVibratePattern = new long[]{0, 400, 1000, 600, 1000, 800, 1000, 1000};
// -1 : Play exactly once
VibrationEffect effect = VibrationEffect.createWaveform(mVibratePattern, -1);
vibrator.vibrate(effect);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createWaveFormVibrationUsingVibrationEffectAndAmplitude() {
long[] mVibratePattern = new long[]{0, 400, 800, 600, 800, 800, 800, 1000};
int[] mAmplitudes = new int[]{0, 255, 0, 255, 0, 255, 0, 255};
// -1 : Play exactly once
if (vibrator.hasAmplitudeControl()) {
VibrationEffect effect = VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, -1);
vibrator.vibrate(effect);
}
}
}
@TimoDJatomika

This comment has been minimized.

Copy link

TimoDJatomika commented Mar 17, 2018

Thanks

@YasserElgammal

This comment has been minimized.

Copy link

YasserElgammal commented Mar 24, 2018

how i can use this with on click button ?

@nieldeokar

This comment has been minimized.

Copy link
Owner Author

nieldeokar commented Apr 11, 2018

Call any method like customVibratePatternNoRepeat() or vibrateFor500ms() from onClick listeners of the button. You can find code for setting button listener here.

@bengaskin

This comment has been minimized.

Copy link

bengaskin commented Apr 11, 2018

Forgive me, I'm new to android development.
I tried the below in MainActivity.java. I got an error

Error:(23, 42) error: vibrateFor500ms() has private access in SimpleVibrateDemoActivity

What did I do wrong? How do I access the private methods from MainActivity.java?


package com.example.nileshdeokar.simplevibratedemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SimpleVibrateDemoActivity.vibrateFor500ms();
            Snackbar.make(view, "Vibrate", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

@ghost

This comment has been minimized.

Copy link

ghost commented Apr 28, 2018

The method vibrateFor500ms in SimpleVibrateDemoActivity class is private change the definition of the method to public in SimpleVibrateDemoActivity.

It is showing you error because it is in different class. One method which is defined as private can be accessed only in that class. U cannot use them in another class. So make it public you use it anywhere in the package .Hope this helps you. 👍

@liangyanmiao

This comment has been minimized.

Copy link

liangyanmiao commented Aug 13, 2018

which phones can return true used “vibrator.hasAmplitudeControl()”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.