Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 465 Vote(s) - 3.38 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make an Android device vibrate? with different frequency?

#11
Kotlin update for more type safety
-----------------------------
Use it as a top level function in some common class of your project such as Utils.kt

// Vibrates the device for 100 milliseconds.
fun vibrateDevice(context: Context) {
val vibrator = getSystemService(context, Vibrator::class.java)
vibrator?.let {
if (Build.VERSION.SDK_INT >= 26) {
it.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
@Suppress("DEPRECATION")
it.vibrate(100)
}
}
}

And then call it anywhere in your code as following:

vibrateDevice(requireContext())

Explanation
-----------

Using `Vibrator::class.java` is more type safe than using `String` constants.

We check the `vibrator` for nullability using `let { }`, because if the vibration is not available for the device, the `vibrator` will be `null`.

It's ok to supress deprecation in `else` clause, because the warning is from newer SDK.

We don't need to ask for permission at runtime for using vibration. But we need to declare it in `AndroidManifest.xml` as following:

<uses-permission android:name="android.permission.VIBRATE"/>
Reply

#12
Vibrate without using permission
---------------------------------

If you want to simply vibrate the device once to provide a feedback on a user action. You can use `performHapticFeedback()` function of a `View`. This doesn't need the `VIBRATE` permission to be declared in the manifest.

Use the following function as a top level function in some common class like Utils.kt of your project:

/**
* Vibrates the device. Used for providing feedback when the user performs an action.
*/
fun vibrate(view: View) {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
}

And then use it anywhere in your `Fragment` or `Activity` as following:

vibrate(requireView())

Simple as that!
Reply

#13
Above answers are perfect. However I wanted to vibrate my app exactly twice on button click and this small information is missing here, hence posting for future readers like me. :)

We have to follow as mentioned above and the only change will be in the vibrate pattern as below,

long[] pattern = {0, 100, 1000, 300};
v.vibrate(pattern, -1); //-1 is important

This will exactly vibrate **twice**. As we already know

1. 0 is for *delay*
2. 100 says *vibrate* for 100ms for the first time
3. next comes *delay* of 1000ms
4. and post that *vibrate* again for 300ms

One can go on and on mentioning delay and vibration alternatively (e.g. 0, 100, 1000, 300, 1000, 300 for 3 vibrations and so on..) but remember @Dave's word use it responsibly. :)

Also note here that the *repeat* parameter is set to -1 which means the vibration will happen **exactly as mentioned in the pattern**. :)
Reply

#14
Vibrating in **Patterns/Waves**:

import android.os.Vibrator;
...
// Pause for 500ms, vibrate for 500ms, then start again
private static final long[] VIBRATE_PATTERN = { 500, 500 };

mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// API 26 and above
mVibrator.vibrate(VibrationEffect.createWaveform(VIBRATE_PATTERN, 0));
} else {
// Below API 26
mVibrator.vibrate(VIBRATE_PATTERN, 0);
}

**Plus**

The necessary permission in `AndroidManifest.xml`:

<uses-permission android:name="android.permission.VIBRATE"/>
Reply

#15

## VIBRATOR_SERVICE is deprecated.
##


----------
So use VIBRATOR_MANAGER_SERVICE in SDK 31 & Above

----------
Make sure you added this permission in AndroidManifest.xml

<uses-permission android:name="android.permission.VIBRATE"/>
----------


Vibrator vibrator;
VibratorManager vibratorManager;
private static final long[] VIBRATE_PATTERN = {500, 500};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT>=31) {
vibratorManager = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
vibrator = vibratorManager.getDefaultVibrator();
}
else {
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}

if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createWaveform(VIBRATE_PATTERN,0));
}
else {
vibrator.vibrate(VIBRATE_PATTERN,0);
}
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through