{"id":11707,"date":"2014-07-18T15:00:41","date_gmt":"2014-07-18T12:00:41","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=11707"},"modified":"2014-08-27T13:19:22","modified_gmt":"2014-08-27T10:19:22","slug":"android-accelerometer-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/","title":{"rendered":"Android Accelerometer Example"},"content":{"rendered":"<p>The Android platform provides several <a href=\"http:\/\/developer.android.com\/guide\/topics\/sensors\/sensors_overview.html\">sensors<\/a> that let you monitor the motion of a device. Some of these sensors are hardware-based and some are software-based.<\/p>\n<p>The Android platform supports three broad categories of sensors:<br \/>\n\u2022\tMotion sensors<br \/>\n\u2022\tEnvironmental sensors thermometers.<br \/>\n\u2022\tPosition sensors<\/p>\n<p><a href=\"http:\/\/developer.android.com\/guide\/topics\/sensors\/sensors_motion.html\">Motion sensors<\/a> are useful for monitoring device movement, such as tilt, shake, rotation, or swing. In our example, we are going to disscuss about a motion \u2013 hardware based sensor, the Android Accelerometer sensor.<br \/>\nThis sensor can measure the acceleration force in m\/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.<\/p>\n<p>So, in this example, we will make an Activity that can listen to Android Accelerometer changes, that can show the current and max x,y,z values, and can vibrate when the accelerometer values are over a certain threshold.<\/p>\n<p>For this tutorial, we will use the following tools in a Windows 64-bit platform:<\/p>\n<ul>\n<li>JDK 1.7<\/li>\n<li>Eclipse 4.2 Juno<\/li>\n<li>Android SDK 4.4<\/li>\n<\/ul>\n<p>Let\u2019s take a closer look:<\/p>\n<h2>1. Create a New Android Application Project<\/h2>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<p>Open Eclipse IDE and go to File \u2192 New \u2192 Project \u2192 Android Application Project.<\/p>\n<p><figure id=\"attachment_11774\" aria-describedby=\"caption-attachment-11774\" style=\"width: 525px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample1.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample1.jpg\" alt=\"File \u2192 New \u2192 Project \u2192 Android Application Project.\" width=\"525\" height=\"500\" class=\"aligncenter size-full wp-image-11774\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample1.jpg 525w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample1-300x285.jpg 300w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><\/a><figcaption id=\"caption-attachment-11774\" class=\"wp-caption-text\">Figure 1. Create a new Android project<\/figcaption><\/figure><\/p>\n<p>Specify the name of the application, the project and the package and then click Next.<\/p>\n<p><figure id=\"attachment_11776\" aria-describedby=\"caption-attachment-11776\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample2.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample2.jpg\" alt=\"Specify the name of the application\" width=\"636\" height=\"545\" class=\"aligncenter size-full wp-image-11776\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample2.jpg 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample2-300x257.jpg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-11776\" class=\"wp-caption-text\">Figure 2. Create a new Android project name<\/figcaption><\/figure><\/p>\n<p>In the next window, the \u201cCreate Activity\u201d option should be checked. The new created activity will be the main activity of your project. Then press Next button.<\/p>\n<p><figure id=\"attachment_11777\" aria-describedby=\"caption-attachment-11777\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample3.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample3.jpg\" alt=\"Create Activity\" width=\"636\" height=\"545\" class=\"aligncenter size-full wp-image-11777\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample3.jpg 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample3-300x257.jpg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-11777\" class=\"wp-caption-text\">Figure 3. Configure the project<\/figcaption><\/figure><\/p>\n<p>In \u201cConfigure Launcher Icon\u201d window you should choose the icon you want to have in your app. We will use the default icon of android, so click Next.<\/p>\n<p><figure id=\"attachment_11778\" aria-describedby=\"caption-attachment-11778\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample4.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample4.jpg\" alt=\"Configure Launcher Icon\" width=\"660\" height=\"590\" class=\"aligncenter size-full wp-image-11778\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample4.jpg 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample4-300x268.jpg 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11778\" class=\"wp-caption-text\">Figure 4. Configure the launcher icon<\/figcaption><\/figure><\/p>\n<p>Select the \u201cBlank Activity\u201d option and press Next.<\/p>\n<p><figure id=\"attachment_11779\" aria-describedby=\"caption-attachment-11779\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample5.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample5.jpg\" alt=\"Blank Activity\" width=\"660\" height=\"590\" class=\"aligncenter size-full wp-image-11779\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample5.jpg 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample5-300x268.jpg 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11779\" class=\"wp-caption-text\">Figure 5. Create the activity and select its type<\/figcaption><\/figure><\/p>\n<p>You have to specify a name for the new Activity and a name for the layout description of your app. The .xml file for the layout will automatically be created in the res\/layout folder. It will also be created a fragment layout xml, that we are not going to use in this project and you can remove it if you want. Then press Finish.<\/p>\n<p><figure id=\"attachment_11780\" aria-describedby=\"caption-attachment-11780\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample6.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample6.jpg\" alt=\"Name for the new Activity\" width=\"660\" height=\"590\" class=\"aligncenter size-full wp-image-11780\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample6.jpg 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample6-300x268.jpg 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11780\" class=\"wp-caption-text\">Figure 6. Create a new blank activity<\/figcaption><\/figure><\/p>\n<p>You can see the structure of the project:<\/p>\n<p><figure id=\"attachment_11781\" aria-describedby=\"caption-attachment-11781\" style=\"width: 356px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7.jpg\" alt=\"The structure of the project\" width=\"356\" height=\"356\" class=\"aligncenter size-full wp-image-11781\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7.jpg 356w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7-150x150.jpg 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7-300x300.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7-200x200.jpg 200w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7-100x100.jpg 100w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample7-42x42.jpg 42w\" sizes=\"(max-width: 356px) 100vw, 356px\" \/><\/a><figcaption id=\"caption-attachment-11781\" class=\"wp-caption-text\">Figure 7. The tree of the project<\/figcaption><\/figure><br \/>\n<span id=\"code\" \/> <\/p>\n<h2>2. Creating the layout of the main Activity<\/h2>\n<p>We are going to make a very simple layout xml for our activity, that only consists of a <code>LinearLayout<\/code> that contains the <code>TextViews<\/code> for the labels and the values of the maximum acceleration values, and three <code>LinearLayouts<\/code> that contain the <code>TextViews<\/code> for the labels and the values of the current acceleration values.<\/p>\n<p>Open <code>res\/layout\/activity_main.xml<\/code>, go to the respective xml tab and paste the following:<\/p>\n<p><em><span style=\"text-decoration: underline\">activity_main.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:layout_gravity=\"center\"\r\n    android:background=\"#ffffff\"\r\n    android:gravity=\"center\"\r\n    android:orientation=\"vertical\"\r\n    android:paddingTop=\"20dp\" &gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginBottom=\"10dp\"\r\n        android:text=\"Max Acceleration:\"\r\n        android:textSize=\"20dp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:text=\"Max Acceleration: X-Axis\"\r\n        android:textSize=\"15dp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/maxX\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"0.0\"\r\n        android:textSize=\"20dp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:text=\"Max Acceleration: Y-Axis\"\r\n        android:textSize=\"15dp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/maxY\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"0.0\"\r\n        android:textSize=\"20dp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:text=\"Max Acceleration: Z-Axis\"\r\n        android:textSize=\"15dp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/maxZ\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"0.0\"\r\n        android:textSize=\"20dp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginBottom=\"10dp\"\r\n        android:layout_marginTop=\"30dp\"\r\n        android:text=\"Current Acceleration:\"\r\n        android:textSize=\"20dp\" \/&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\"\r\n        android:background=\"#ffffff\"\r\n        android:gravity=\"center|top\"\r\n        android:orientation=\"horizontal\" &gt;\r\n\r\n        &lt;LinearLayout\r\n            android:layout_width=\"0dp\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_weight=\"0.33\"\r\n            android:background=\"#ffffff\"\r\n            android:gravity=\"center\"\r\n            android:orientation=\"vertical\" &gt;\r\n\r\n            &lt;TextView\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_gravity=\"center\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:text=\"X-Axis\"\r\n                android:textSize=\"15dp\" \/&gt;\r\n\r\n            &lt;TextView\r\n                android:id=\"@+id\/currentX\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_gravity=\"center\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:text=\"0.0\"\r\n                android:textSize=\"15dp\" \/&gt;\r\n        &lt;\/LinearLayout&gt;\r\n\r\n        &lt;LinearLayout\r\n            android:layout_width=\"0dp\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_weight=\"0.33\"\r\n            android:background=\"#ffffff\"\r\n            android:gravity=\"center\"\r\n            android:orientation=\"vertical\" &gt;\r\n\r\n            &lt;TextView\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_gravity=\"center\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:text=\"Y-Axis\"\r\n                android:textSize=\"15dp\" \/&gt;\r\n\r\n            &lt;TextView\r\n                android:id=\"@+id\/currentY\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_gravity=\"center\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:text=\"0.0\"\r\n                android:textSize=\"15dp\" \/&gt;\r\n        &lt;\/LinearLayout&gt;\r\n\r\n        &lt;LinearLayout\r\n            android:layout_width=\"0dp\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_weight=\"0.33\"\r\n            android:background=\"#ffffff\"\r\n            android:gravity=\"center\"\r\n            android:orientation=\"vertical\" &gt;\r\n\r\n            &lt;TextView\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_gravity=\"center\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:text=\"Z-Axis\"\r\n                android:textSize=\"15dp\" \/&gt;\r\n\r\n            &lt;TextView\r\n                android:id=\"@+id\/currentZ\"\r\n                android:layout_width=\"wrap_content\"\r\n                android:layout_height=\"wrap_content\"\r\n                android:layout_gravity=\"center\"\r\n                android:layout_marginTop=\"10dp\"\r\n                android:text=\"0.0\"\r\n                android:textSize=\"15dp\" \/&gt;\r\n        &lt;\/LinearLayout&gt;\r\n    &lt;\/LinearLayout&gt;\r\n\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<h2>3. Creating the source code of the main Activity<\/h2>\n<p>Open <code>src\/com.javacodegeeks.androidaccelerometerexample\/AndroidVideoViewExample.java<\/code> file and paste the code below.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><em><span style=\"text-decoration: underline\">AndroidAccelerometerExample .java<\/span><\/em><\/p>\n<pre class=\"brush:java; wrap-lines:false\">\r\npackage com.javacodegeeks.androidaccelerometerexample;\r\n\r\nimport android.app.Activity;\r\nimport android.content.Context;\r\nimport android.hardware.Sensor;\r\nimport android.hardware.SensorEvent;\r\nimport android.hardware.SensorEventListener;\r\nimport android.hardware.SensorManager;\r\nimport android.os.Bundle;\r\nimport android.os.Vibrator;\r\nimport android.widget.TextView;\r\n\r\npublic class AndroidAccelerometerExample extends Activity implements SensorEventListener {\r\n\r\n\tprivate float lastX, lastY, lastZ;\r\n\r\n\tprivate SensorManager sensorManager;\r\n\tprivate Sensor accelerometer;\r\n\r\n\tprivate float deltaXMax = 0;\r\n\tprivate float deltaYMax = 0;\r\n\tprivate float deltaZMax = 0;\r\n\r\n\tprivate float deltaX = 0;\r\n\tprivate float deltaY = 0;\r\n\tprivate float deltaZ = 0;\r\n\r\n\tprivate float vibrateThreshold = 0;\r\n\r\n\tprivate TextView currentX, currentY, currentZ, maxX, maxY, maxZ;\r\n\r\n\tpublic Vibrator v;\r\n\r\n\t@Override\r\n\tpublic void onCreate(Bundle savedInstanceState) {\r\n\t\tsuper.onCreate(savedInstanceState);\r\n\t\tsetContentView(R.layout.activity_main);\r\n\t\tinitializeViews();\r\n\r\n\t\tsensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);\r\n\t\tif (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {\r\n\t\t\t\/\/ success! we have an accelerometer\r\n\r\n\t\t\taccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);\r\n\t\t\tsensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);\r\n\t\t\tvibrateThreshold = accelerometer.getMaximumRange() \/ 2;\r\n\t\t} else {\r\n\t\t\t\/\/ fai! we dont have an accelerometer!\r\n\t\t}\r\n\t\t\r\n\t\t\/\/initialize vibration\r\n\t\tv = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);\r\n\r\n\t}\r\n\r\n\tpublic void initializeViews() {\r\n\t\tcurrentX = (TextView) findViewById(R.id.currentX);\r\n\t\tcurrentY = (TextView) findViewById(R.id.currentY);\r\n\t\tcurrentZ = (TextView) findViewById(R.id.currentZ);\r\n\r\n\t\tmaxX = (TextView) findViewById(R.id.maxX);\r\n\t\tmaxY = (TextView) findViewById(R.id.maxY);\r\n\t\tmaxZ = (TextView) findViewById(R.id.maxZ);\r\n\t}\r\n\r\n\t\/\/onResume() register the accelerometer for listening the events\r\n\tprotected void onResume() {\r\n\t\tsuper.onResume();\r\n\t\tsensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);\r\n\t}\r\n\r\n\t\/\/onPause() unregister the accelerometer for stop listening the events\r\n\tprotected void onPause() {\r\n\t\tsuper.onPause();\r\n\t\tsensorManager.unregisterListener(this);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void onAccuracyChanged(Sensor sensor, int accuracy) {\r\n\t\t\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void onSensorChanged(SensorEvent event) {\r\n\r\n\t\t\/\/ clean current values\r\n\t\tdisplayCleanValues();\r\n\t\t\/\/ display the current x,y,z accelerometer values\r\n\t\tdisplayCurrentValues();\r\n\t\t\/\/ display the max x,y,z accelerometer values\r\n\t\tdisplayMaxValues();\r\n\r\n\t\t\/\/ get the change of the x,y,z values of the accelerometer\r\n\t\tdeltaX = Math.abs(lastX - event.values[0]);\r\n\t\tdeltaY = Math.abs(lastY - event.values[1]);\r\n\t\tdeltaZ = Math.abs(lastZ - event.values[2]);\r\n\r\n\t\t\/\/ if the change is below 2, it is just plain noise\r\n\t\tif (deltaX &lt; 2)\r\n\t\t\tdeltaX = 0;\r\n\t\tif (deltaY &lt; 2)\r\n\t\t\tdeltaY = 0;\r\n\t\tif (deltaZ  vibrateThreshold) || (deltaY &gt; vibrateThreshold) || (deltaZ &gt; vibrateThreshold)) {\r\n\t\t\tv.vibrate(50);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void displayCleanValues() {\r\n\t\tcurrentX.setText(\"0.0\");\r\n\t\tcurrentY.setText(\"0.0\");\r\n\t\tcurrentZ.setText(\"0.0\");\r\n\t}\r\n\r\n\t\/\/ display the current x,y,z accelerometer values\r\n\tpublic void displayCurrentValues() {\r\n\t\tcurrentX.setText(Float.toString(deltaX));\r\n\t\tcurrentY.setText(Float.toString(deltaY));\r\n\t\tcurrentZ.setText(Float.toString(deltaZ));\r\n\t}\r\n\r\n\t\/\/ display the max x,y,z accelerometer values\r\n\tpublic void displayMaxValues() {\r\n\t\tif (deltaX &gt; deltaXMax) {\r\n\t\t\tdeltaXMax = deltaX;\r\n\t\t\tmaxX.setText(Float.toString(deltaXMax));\r\n\t\t}\r\n\t\tif (deltaY &gt; deltaYMax) {\r\n\t\t\tdeltaYMax = deltaY;\r\n\t\t\tmaxY.setText(Float.toString(deltaYMax));\r\n\t\t}\r\n\t\tif (deltaZ &gt; deltaZMax) {\r\n\t\t\tdeltaZMax = deltaZ;\r\n\t\t\tmaxZ.setText(Float.toString(deltaZMax));\r\n\t\t}\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p>Let&#8217;s see in detail the code above.<\/p>\n<p>We set the <code>activity_main.xml<\/code> layout and we initialize our basic views by: <\/p>\n<pre class=\"brush:java; wrap-lines:false\">setContentView(R.layout.activity_main);\r\ninitializeViews();<\/pre>\n<p>Then, we want to check if our device supports the accelerometer sensor, and can run the specific service. If our device supports accerelometer, we initialize our sensorManager in order to bind the sensor to listen to the accelerometer events. So, we register a specific listener to catch the events by writing in the <code>onCreate()<\/code> method the following lines:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\r\n\t\tsensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);\r\n\t\tif (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {\r\n\t\t\t\/\/ success! we have an accelerometer\r\n\r\n\t\t\taccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);\r\n\t\t\tsensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);\r\n\t\t\tvibrateThreshold = accelerometer.getMaximumRange() \/ 2;\r\n\t\t} else {\r\n\t\t\t\/\/ fai! we dont have an accelerometer!\r\n\t\t}\r\n<\/pre>\n<p>Generally, we do not want these kind of listeners to run to our device continually, because most of the times they appear to be energy and cpu consuming, so we are going to unregister our listener <code>onPause()<\/code> of our activity, and then register them back on, <code>onResume()<\/code> of our activity. So, we have to add:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\r\n\t\/\/onResume() register the accelerometer for listening the events\r\n\tprotected void onResume() {\r\n\t\tsuper.onResume();\r\n\t\tsensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);\r\n\t}\r\n<\/pre>\n<pre class=\"brush:java; wrap-lines:false\">\r\n\t\/\/onPause() unregister the accelerometer for stop listening the events\r\n\tprotected void onPause() {\r\n\t\tsuper.onPause();\r\n\t\tsensorManager.unregisterListener(this);\r\n\t}\r\n<\/pre>\n<p>We, have now come to the point, that we should get the accelerometer events, and use them. We are going to get the x,z,y events, by using the <code>event.values[0], event.values[1], event.values[2]<\/code> accordingly. In fact, we want to save the difference between the values of two consecutive events, because this is metric that is going to show us, if we have shaked or tilted our mobile device. We have also to keep in mind, that this change might be almost impercetible for our human sences, but our mobile device will calculate even the slightest diffence, so we have to set a low threshold value, that will be considered as noise, and therefore will not be calculated by our device. <\/p>\n<pre class=\"brush:java; wrap-lines:false\">\r\n\t@Override\r\n\tpublic void onSensorChanged(SensorEvent event) {\r\n\r\n\t\t\/\/ clean current values\r\n\t\tdisplayCleanValues();\r\n\t\t\/\/ display the current x,y,z accelerometer values\r\n\t\tdisplayCurrentValues();\r\n\t\t\/\/ display the max x,y,z accelerometer values\r\n\t\tdisplayMaxValues();\r\n\r\n\t\t\/\/ get the change of the x,y,z values of the accelerometer\r\n\t\tdeltaX = Math.abs(lastX - event.values[0]);\r\n\t\tdeltaY = Math.abs(lastY - event.values[1]);\r\n\t\tdeltaZ = Math.abs(lastZ - event.values[2]);\r\n\r\n\t\t\/\/ if the change is below 2, it is just plain noise\r\n\t\tif (deltaX &lt; 2)\r\n\t\t\tdeltaX = 0;\r\n\t\tif (deltaY &lt; 2)\r\n\t\t\tdeltaY = 0;\r\n\t\tif (deltaZ &lt; 2)\r\n\t\t\tdeltaZ = 0;\r\n\r\n\t\t\/\/ set the last know values of x,y,z\r\n\t\tlastX = event.values[0];\r\n\t\tlastY = event.values[1];\r\n\t\tlastZ = event.values[2];\r\n\r\n\t\tvibrate();\r\n\t}\r\n<\/pre>\n<p>Our device will now vibrate, if we shake it enough! This will happen, only if we add in our code the line:<\/p>\n<pre class=\"brush:java; wrap-lines:false;highlight:[11]\">\t\/\/ if the change in the accelerometer value is big enough, then vibrate!\r\n\t\/\/ our threshold is MaxValue\/2\r\n\tpublic void vibrate() {\r\n\t\tif ((deltaX &gt; vibrateThreshold) || (deltaY &gt; vibrateThreshold) || (deltaZ &gt; vibrateThreshold)) {\r\n\t\t\tv.vibrate(50);\r\n\t\t}\r\n\t}\r\n\r\n<\/pre>\n<h2>4. Android Manifest<\/h2>\n<p>In order to enable the vibration feature, we have to grant permission to our application to use vibration. So, we have to add in the <\/code>AndroidManifest.xml<\/code> the following line: <code>&lt;uses-permission android:name=\"android.permission.VIBRATE\" \/&gt;\"<\/code><\/p>\n<p>We have also blocked our orientation to portait by adding the line: <code>android:screenOrientation=\"portrait\"<\/code><\/p>\n<p><em><span style=\"text-decoration: underline\">AndroidManifest.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false; highlight:[5]\">    \r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    package=\"com.javacodegeeks.androidaccelerometerexample\"\r\n    android:versionCode=\"1\"\r\n    android:versionName=\"1.0\" &gt;\r\n\r\n    &lt;uses-sdk\r\n        android:minSdkVersion=\"8\"\r\n        android:targetSdkVersion=\"19\" \/&gt;\r\n\r\n    &lt;uses-permission android:name=\"android.permission.VIBRATE\" \/&gt;\r\n\r\n    &lt;application\r\n        android:allowBackup=\"true\"\r\n        android:icon=\"@drawable\/ic_launcher\"\r\n        android:label=\"@string\/app_name\" &gt;\r\n        &lt;activity\r\n            android:name=\"com.javacodegeeks.androidaccelerometerexample.AndroidAccelerometerExample\"\r\n            android:configChanges=\"keyboardHidden|orientation|screenSize\"\r\n            android:label=\"@string\/app_name\"\r\n            android:screenOrientation=\"portrait\" &gt;\r\n            &lt;intent-filter&gt;\r\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n\r\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n            &lt;\/intent-filter&gt;\r\n        &lt;\/activity&gt;\r\n    &lt;\/application&gt;\r\n\r\n&lt;\/manifest&gt;\r\n       \r\n<\/pre>\n<h2>5. Build, compile and run<\/h2>\n<p>When we build, compile and run our project, the main Activity should look like this:<\/p>\n<p><figure id=\"attachment_11833\" aria-describedby=\"caption-attachment-11833\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-5.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-5.jpg\" alt=\"If we lay our mobile device on a table, this is metric that we will see. It is the z acceleration value and it is equal the force of gravity ( about 9.81 m\/s2).\" width=\"450\" height=\"800\" class=\"size-full wp-image-11833\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-5.jpg 450w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-5-168x300.jpg 168w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-11833\" class=\"wp-caption-text\">If we lay our mobile device on a table, this is metric that we will see. It is the z acceleration value and it is equal the force of gravity ( about 9.81 m\/s2).<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_11832\" aria-describedby=\"caption-attachment-11832\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-4.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-4.jpg\" alt=\"If we keep our mobile device on vertical position, this is metric that we will see. It is the y acceleration value and it is equal the force of gravity ( about 9.81 m\/s2).\" width=\"450\" height=\"800\" class=\"size-full wp-image-11832\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-4.jpg 450w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-4-168x300.jpg 168w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-11832\" class=\"wp-caption-text\">If we keep our mobile device on vertical position, this is metric that we will see. It is the y acceleration value and it is equal the force of gravity ( about 9.81 m\/s2).<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_11829\" aria-describedby=\"caption-attachment-11829\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-1.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-1.jpg\" alt=\"If we keep our mobile device on horizontal position, this is metric that we will see. It is the x acceleration value and it is equal the force of gravity ( about 9.81 m\/s2).\" width=\"450\" height=\"800\" class=\"size-full wp-image-11829\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-1.jpg 450w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-1-168x300.jpg 168w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-11829\" class=\"wp-caption-text\">If we keep our mobile device on horizontal position, this is metric that we will see. It is the x acceleration value and it is equal the force of gravity ( about 9.81 m\/s2).<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_11831\" aria-describedby=\"caption-attachment-11831\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-3.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-3.jpg\" alt=\"This is the main screen of the activity.\" width=\"450\" height=\"800\" class=\"size-full wp-image-11831\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-3.jpg 450w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerUI-3-168x300.jpg 168w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-11831\" class=\"wp-caption-text\">This is the main screen of the activity.<\/figcaption><\/figure><\/p>\n<h2>Download the Eclipse Project<\/h2>\n<p>This was an example of Android Accelerometer. <\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/AndroidAccelerometerExample.zip\">AndroidAccelerometerExample.zip<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Android platform provides several sensors that let you monitor the motion of a device. Some of these sensors are hardware-based and some are software-based. The Android platform supports three broad categories of sensors: \u2022 Motion sensors \u2022 Environmental sensors thermometers. \u2022 Position sensors Motion sensors are useful for monitoring device movement, such as tilt, &hellip;<\/p>\n","protected":false},"author":14,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[628],"tags":[],"class_list":["post-11707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sensor"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android Accelerometer Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"The Android platform provides several sensors that let you monitor the motion of a device. Some of these sensors are hardware-based and some are\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Accelerometer Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"The Android platform provides several sensors that let you monitor the motion of a device. Some of these sensors are hardware-based and some are\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-07-18T12:00:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-08-27T10:19:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Chryssa Aliferi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Chryssa Aliferi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/\"},\"author\":{\"name\":\"Chryssa Aliferi\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\"},\"headline\":\"Android Accelerometer Example\",\"datePublished\":\"2014-07-18T12:00:41+00:00\",\"dateModified\":\"2014-08-27T10:19:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/\"},\"wordCount\":1026,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"articleSection\":[\"Sensor\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/\",\"name\":\"Android Accelerometer Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"datePublished\":\"2014-07-18T12:00:41+00:00\",\"dateModified\":\"2014-08-27T10:19:22+00:00\",\"description\":\"The Android platform provides several sensors that let you monitor the motion of a device. Some of these sensors are hardware-based and some are\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"core\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"hardware\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/hardware\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Sensor\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/hardware\/sensor\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Android Accelerometer Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\",\"name\":\"Chryssa Aliferi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"caption\":\"Chryssa Aliferi\"},\"description\":\"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Accelerometer Example - Java Code Geeks","description":"The Android platform provides several sensors that let you monitor the motion of a device. Some of these sensors are hardware-based and some are","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/","og_locale":"en_US","og_type":"article","og_title":"Android Accelerometer Example - Java Code Geeks","og_description":"The Android platform provides several sensors that let you monitor the motion of a device. Some of these sensors are hardware-based and some are","og_url":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-07-18T12:00:41+00:00","article_modified_time":"2014-08-27T10:19:22+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","type":"image\/jpeg"}],"author":"Chryssa Aliferi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Chryssa Aliferi","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/"},"author":{"name":"Chryssa Aliferi","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332"},"headline":"Android Accelerometer Example","datePublished":"2014-07-18T12:00:41+00:00","dateModified":"2014-08-27T10:19:22+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/"},"wordCount":1026,"commentCount":4,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","articleSection":["Sensor"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/","url":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/","name":"Android Accelerometer Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","datePublished":"2014-07-18T12:00:41+00:00","dateModified":"2014-08-27T10:19:22+00:00","description":"The Android platform provides several sensors that let you monitor the motion of a device. Some of these sensors are hardware-based and some are","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/hardware\/sensor\/android-accelerometer-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/"},{"@type":"ListItem","position":3,"name":"core","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/"},{"@type":"ListItem","position":4,"name":"hardware","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/hardware\/"},{"@type":"ListItem","position":5,"name":"Sensor","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/hardware\/sensor\/"},{"@type":"ListItem","position":6,"name":"Android Accelerometer Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332","name":"Chryssa Aliferi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","caption":"Chryssa Aliferi"},"description":"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=11707"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11707\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=11707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=11707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=11707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}