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
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
- How to check your installed version of Git - Git
- Notepad++ select all above or below lines - NotepadPlusPlus
- [Mac] How to Mute People in a Microsoft Teams Meeting - Teams
- Android Studio Button onClickListener Example - Android-Studio
- How to come out of Full Screen Mode Roblox on Mac - MacOS
- How to change background color in Notepad++ - NotepadPlusPlus
- Mac - Open Finder App using Terminal Current Location - MacOS
- How to change SharePoint Online site collection or subsite URL address - SharePoint