Hello! So this weekend, we will see how to make our device vibrate and detect shaking using android studio.
Shake detection and vibration are very important for an android app related to communication or lifestyle so the apps having such features are more popular on the app stores. So today we are going to implement a vibration mechanism in our app and a shake detection feature.
So, let's get started with the code.
Introduction
Everyone is familiar with what is vibration and shaking. But to clear some concepts read the paras given below or jump to the code directly.
Vibration in Android
Most 21st-century mobile phones are fitted with a vibrating alert.
Vibration is a mechanical phenomenon whereby oscillations occur about an equilibrium point. The word comes from Latin vibrationem ("shaking, brandishing"). The oscillations may be periodic, such as the motion of a pendulum—or random, such as the movement of a tire on a gravel road. source: wikipedia.
Vibrating alerts are primarily used when a user cannot hear the ring tone (phone in pocket) Or wants a more discreet notification (such as in a theatre).
Shake Detection in Android
Sometimes you might wanna do something when the user shakes the phone
Maybe taking a screenshot
Or rolling a dice in a game
shaking refers to move (an object) up and down or from side to side with rapid, forceful, jerky movements. source: google
So we are done with the theory Now let's start coding.
The Code
here we are going to add some dependencies while coding so make sure you have a stable and good internet connection.
Vibration
Before you start coding you have to add the permission in the android manifest file. Same as we do while adding internet permission add the below line to your manifest.xml.
<uses-permission android:name="android.permission.VIBRATE"/>
Now add the following code to your MainActivity.kt file (because I'm using kotlin here) to get an instance of the vibrator:
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
To produce vibrations, the device must have a vibrator. To check this use the following code so we will know if the device has a vibrator or not and produce some log or toast in the if-else statement to know the status of the vibrator.
if (vibrator.hasVibrator()) {
// start vibration
}
else {
// device has no vibration feature
}
Now to produce vibrations, Create vibration patterns by passing in an array of longs, each of which represents a duration in milliseconds.
The first number is the start time delay. Each array entry then alternates between vibrate, sleep, vibrate, sleep, etc.
// vibrate 100 milliseconds and sleep 1000 milliseconds
// vibrate 200 milliseconds and sleep 2000 milliseconds
val vibrationPattern= longArrayOf(0, 100, 1000, 200, 2000)
// Start the vibration
vibrator.vibrate(vibrationPattern, -1) // does not repeat
vibrator.vibrate(vibrationPattern, 0) // repeats forever
Now your app is ready to make vibrations. If you used this in your app put your app link in the comments for me.
Now let's see how to detect shaking in Android.
Shake Detection
For shake detection, you have to add the seismic library to your app-level build.gradle.
dependencies {
// seismic
implementation 'com.squareup:seismic:1.0.2'
}
Now you can use the SensorManager and ShakeDetector to detect the shake. Use the following code to implement it in your MainActivity.kt file.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
shakeDetector = ShakeDetector(ShakeDetector.Listener {
Toast.makeText(this, "Shake detected!", Toast.LENGTH_SHORT)
.show()
})
}
override fun onResume() {
super.onResume()
shakeDetector.start(sensorManager)
}
override fun onPause() {
super.onPause()
shakeDetector.stop()
}
Now when you try to shake your device when your app is running a toast will be displayed.
Detecting the shake with Android Studio
Most of us use the android emulator from the android studio for testing our apps and shake detection can also be done using the emulator.
Go to emulator settings → virtual sensors → select move option
Then move the x-axis seek bar from left to right repeatedly.
Toast will be shown when the shake is detected!
Wrapping Up
Shake detection and vibration are mostly used in communication, lifestyle or in the apps we use every day like callers, alarms etc. and if you are making an app using it be share to share the app link in the comments if you have uploaded it on AppStore.
Tips
Just some tips to give you an idea of what else can you improve or do with this in your app.
- As I said you can use it in an alarm or caller app.
- Shake detection can be used as an advanced feature as a gesture to do something.
- Try using such features in your apps to make them more advanced.
- and last Support me on buy me a coffee🤗.
Thanks for reading.
Reference:
https://github.com/square/seismic
https://wikipedia.org
https://stackoverflow.com
Top comments (0)