Android Shake Detection Tutorial Example Sensor Listener Accelerometer

Android shake detection tutorial with example is what you will learn today.

System of Android gives facility to detect motion sensor listener with accelerometer class.

You can define various code on shake function of your android device that will work when user shakes the device.

It is something that is unique way to stand out from old traditional methods like button click.

For example, you are making a saloon booking application. Now you can have a feature like when the user shakes his device system will return near by saloons.

In this tutorial, we will see how to detect when user shake his android device. We will change the text of the TextView on the the shake function detection.

We can also do this task with the help of background service. Detection service will constantly watch out for shake activity and it will run specific code when it finds it.

I have used android studio to develop this tutorial example.

Two Ways

You can implement shake feature in two ways.

  • First way is to have one detection class that we will use to detect the shake in any particular activity.

This method can not be used in that way that you write once and use everywhere. You have to write detection code in every activity in this method.

  • Second method is to create a background service. Make sensor registration in this service. This service will constantly check for shake action.

Here, you do not need to write sensor registration code everywhere. Just create one service and start it in any activity. That’s it.

We will implement both this feature in this example.

So let us generate shake example step by step.

Step 1. Detector Class

Let us make a class that will detect a shake task of the user.

Create a new class and give it a name “ShakeDetector.java”

Copy and paste the below source code in “ShakeDetector.java”

Step 2. Detect Service

We are also implementing one feature in which a background service is running constantly.

Whenever the user will shake his android device, this service detect this and will execute specific code.

So for developing this service, make a new java class named “ShakeService.java”

Write down the following coding lines in “ShakeService.java”

Consider the below source code

  • Compiler will execute the above code when service detects the shake action.
  • It will create a random color first and then it will set this color as a text color of the textview.
  • This textview is present in the ServiceActivity which we will create in the next step.
  • If you probably know that every activity is defined in the manifest by the compiler when you create them.
  • Similarly, we also need to define this service in the AndroidManifest.xml file.

Add following line in AndroidManifest.xml file (between <application>…</application> tags).

Final code for AndroidManifest.xml file looks like the below

Step 3. Activity to Start Service

Now we need to make one activity in which we will start the background service.

Create a new activity named “ServiceActivity”

Add the below source code in activity_service.xml file

  • I have taken one TextView in this file.
  • We will change the text color on every shake detection.

Copy the following source code in ServiceActivity.java class

Following code will start the background service

Step 4. Changing Main Activity

Now last thing is to change the code for Main Activity files.

Write the below code in activity_main.xml file

  • Here I have taken one button and one textview.

Write the following code in MainActivity.java file

Let us understand the above code.

Look at the below source code

  • We will open a ServiceActivity on the button click.

Consider the following code

  • Above code will initialize the detection through the ShakeDector class.
  • Whenever the system detects the Shake action, it will execute the onShake() method.
  • onShake() method will update the text of the TextView.
  • Then it will show a toast saying “Shaked!”

Now give your attention to the below code

  • Generally, compiler call the onResume() method when activity is opening for first time or it is returning from another application.
  • Compiler will register the sensor manager in this method.

See the following source code

  • System will run the onPause() method when the activity is destroyed or user directly moves to another application without killing our app.
  • Compiler will cancel the registration of sensor in this method.

Demo Video

Following video demonstrate the output of this example.

Download Source Code for Android Shake Detection Example

[sociallocker]Download Shake Detection Example Source Code[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *