Last active
September 1, 2023 11:04
-
-
Save nieldeokar/e05fffe4d639dfabf0d57e96cb8055e2 to your computer and use it in GitHub Desktop.
Android Vibrate & VibrationEffect class demo Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
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
The method
vibrateFor500ms
inSimpleVibrateDemoActivity
class is private change the definition of the method topublic
inSimpleVibrateDemoActivity
.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. 👍