You make have a project requirement to implement a feature that activates when the user Shakes his/her Android Phone. Say you want to reload the Activity contents when the user shakes the phone. To implement such functionality you need to create a listener that is related to motion and the hardware that deals with motion is called the Accelerator.
Accelerator is a motion sensor (hardware) that is built into your Android device that can measure motion and orientations. They provide us raw data with high precision and accuracy. Most of the Gaming Application makes use of this sensor.
Before we learn how to implement Shake functionality it is important to know that what minSDK level is Accelerator sensor available and does the device supports it. This sensor is available from early API Level 3 i.e. Android 1.5 Cupcake. Now let's see the code to detect if Accelerator is available on the device or not,
Snippet: DetectSensor.java//This is a code to detect if ACCELEROMETER sensor is available on device or not
SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List listOfSensorsOnDevice = mSensorManager.getSensorList(Sensor.TYPE_ALL);
for (int i = 0; i< listOfSensorsOnDevice.size(); i++) {
if (listOfSensorsOnDevice.get(i).getType() == Sensor.TYPE_ACCELEROMETER) {
Toast.makeText(this,"TYPE_ACCELEROMETER sensor is available on device",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this,"TYPE_ACCELEROMETER sensor is NOT available on device",Toast.LENGTH_SHORT).show();
}
}
SensorManager is the class that we have to make use of to detect sensor availability. Just add the above code in the onCreate() function of the launcher Activity it check if ACCELEROMETER sensor is available.
Note : ACCELEROMETER sensor is not available on Emulator so do check this code on your handheld device.
Example: Detect Android Shake eventLet's take a very simple example, We will create a simple application with only one Activity containing a TextView. When the user shakes the phone the TextView text is updated to display the count i.e. number of times the shake event occurred.
File : activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/counter" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:id="@+id/counter" />
</RelativeLayout>
File : MainActivity.java
package com.code2care.tools.detectphoneshakes;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends ActionBarActivity implements SensorEventListener {
int count = 1;
private boolean init;
private Sensor mAccelerometer;
private SensorManager mSensorManager;
private float x1, x2, x3;
private static final float ERROR = (float) 7.0;
private TextView counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (TextView) findViewById(R.id.counter);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List listOfSensorsOnDevice = mSensorManager.getSensorList(Sensor.TYPE_ALL);
for (int i = 0; i < listOfSensorsOnDevice.size(); i++) {
if (listOfSensorsOnDevice.get(i).getType() == Sensor.TYPE_ACCELEROMETER) {
Toast.makeText(this, "ACCELEROMETER sensor is available on device", Toast.LENGTH_SHORT).show();
init = false;
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
} else {
Toast.makeText(this, "ACCELEROMETER sensor is NOT available on device", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onSensorChanged(SensorEvent e) {
//Get x,y and z values
float x,y,z;
x = e.values[0];
y = e.values[1];
z = e.values[2];
if (!init) {
x1 = x;
x2 = y;
x3 = z;
init = true;
} else {
float diffX = Math.abs(x1 - x);
float diffY = Math.abs(x2 - y);
float diffZ = Math.abs(x3 - z);
//Handling ACCELEROMETER Noise
if (diffX < ERROR) {
diffX = (float) 0.0;
}
if (diffY < ERROR) {
diffY = (float) 0.0;
}
if (diffZ < ERROR) {
diffZ = (float) 0.0;
}
x1 = x;
x2 = y;
x3 = z;
//Horizontal Shake Detected!
if (diffX > diffY) {
counter.setText("Shake Count : "+ count);
count = count+1;
Toast.makeText(MainActivity.this, "Shake Detected!", Toast.LENGTH_SHORT).show();
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//Noting to do!!
}
//Register the Listener when the Activity is resumed
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
//Unregister the Listener when the Activity is paused
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
Related Questions
- How to detect Horizontal shake motions in Android?
- Detect Shakes using Android Accelerometer?
- Android Shake detector source code?
- How to check if Accelerometer is available on Android device ?
- Detect Shake Gesture in Android?
HashTags : #Android #Shake #ShakeDetection #MotionSensors #Sensors #Accelerometer
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
- How to make TextEdit the default text Editor on Mac - MacOS
- Test Timeout using JUnit 4 with examples - Java
- What is Terminal Velocity and its Formula? How to calculate it programmatically? - Python
- Fix - 412 Cookies Are Disabled error in Exchange Admin Center when you click hybrid - Microsoft
- Pass data between two Android Activities and access it using Intent - Android
- Python Sleep Function/Method Code Example - Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11 - Python
- [Fix] brew: command not found Mac or Linux Terminal Error - MacOS