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
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- M365 service Europe outage - AADSTS90033 A transient error has occurred. Please try again. - Microsoft
- Error code 0xCAA82EE2: Something went wrong (request timed out) [Microsoft] - Microsoft
- Make Notepad++ the default App for .txt file extensions - NotepadPlusPlus
- How to find files taking up space on your Mac - MacOS
- How to fix: Please check your network connection (Retry) - HowTos
- [Android Studio] Button on click example - Android-Studio
- Make Android View Scrollable both Horizontally and Vertically - Android
- Java JDBC Example with Oracle Database Driver Connection - Java