">
5- By entering the above codes, we define the permission to access the vibration capability in the project manifest.
This license is not Dangerous Permissions, so we do not define it in Runtime Permission.
6- The codes that should be in the AndroidManifest.xml section are as follows:
To create a normal vibration, we must first create a Button inside the active layout.
Of course, it should be noted that normal vibration is such that it creates a long and simple vibration for a while.
7- The codes that should be in the activity_main.xml section are as follows:
8- Then, we have to complete the activity as follows.
9- The codes that should be in the MainActivity.java section are as follows:
In the code above, we created an example of a Vibrator with the custom name of mVibrator. We then defined the vibration API inside the onCreate () method. Like the following:
A listener is then defined for the button, which contains the vibrate () method. This method can take a long input and specify the duration of the device vibration in milliseconds. The number selected in this part of the tutorial and the codes listed above is 300, which can cause the device to vibrate for 0.3.
The definition of vibration is deprecated for the version of Android operating systems, so we must rewrite the listed methods for Android versions O and above.
VibrationEffect.createOneShot creates a continuous, long vibration in the code above.
In the vibration API of Android devices, in addition to the possibility of applying simple vibrations, it is also possible to apply patterned vibrations, and different types of patterned vibrations can be applied.
To do this, we define a long variable inside the body of the active class.
In the codes listed above, we have defined a variable that has a representation. An array of different numbers. These numbers indicate the duration of pause and vibration, respectively.
It should be noted that there are no restrictions on determining and applying numbers to this presentation, and you can enter any number of elements into this presentation.
10- Add another button to the layout and complete the Listener method as follows:
In the code above, VibrationEffect.createWaveform creates a patterned vibration. The first input is made of long, which we have already defined. The second input also specifies its repetition or non-repetition. It should be noted that if we enter the value of -1 in the second input, the vibration will not be repeated. Still, if we enter the value of 1, the vibration pattern will continue until we send the cancellation command.
11- Using VibrationEffect, we can use predefined effects. These effects include EFFECT_DOUBLE_CLICK, EFFECT_CLICK, EFFECT_HEAVY_CLICK, and EFFECT_TICK.
12- Complete the condition inside the Listener as follows:
After entering the codes listed above, if we run the project and the Android device is version 10 or higher, the condition listed in the first part of the codes will be executed, and there will be an EFFECT_DOUBLE_CLICK vibration. This vibration is actually like a double click, so it is called pattern vibration. And if the Android version is not ten or higher, the conditions listed in the following sections will be applied.
13- The complete and final codes that should be in the activity_main.xml section are as follows:
14- The complete and final code that should be in the MainActivity.java section are as follows:
In addition to the possibility of creating and creating vibration, it is also possible to cancel and cancel the vibration, for which we must do the following:
The vibration pattern defined above has a delay of 0 milliseconds at the beginning and end, so if this pattern continues, it will be a continuous and long vibration.
15- In the first part, we deactivate the condition we entered in the second part.
In the codes listed above, we replace the second pattern, vibrate pattern, as the first input and the number zero as the second input. Unlike the previous part, the vibration pattern of this part will be repeated infinitely.
16- Add another button called "Cancel" to the layout. Like the following:
The code above uses the hasVibrator () method to check if a vibrator runs. And after checking this method, if the result is true, that is, if the vibration is running, the cancel () method will be executed to cancel the vibration.
17- If we run the project and press the second button, the vibration will start and be repeated infinitely, and if we press the third button, the vibration will be canceled.
18- The complete and final codes that should be in the activity_main.xml section are as follows:
19- The code that should be in the MainActivity.java section is as follows:
In this tutorial, you will learn how to create a simple vibration, patterned vibration and how to cancel vibrations. If you do all the steps mentioned in this tutorial with high accuracy, you can create vibrations and cancel them if necessary.
About our Android and iOS app development services