Android Acceleration Sensor

Source: Internet
Author: User

Android Acceleration Sensor

Two screenshots of the mobile phone flat desktop, the data has been refreshed


Source

(Android Studio project): http://download.csdn.net/detail/q4878802/9065313

Steps

Sensor use steps have been introduced before, address: http://blog.csdn.net/q4878802/article/details/48112477

Code
 PackageCom.example.kongqw.kqwsensorforaccelerometerdemo;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.hardware.Sensor;ImportAndroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public  class mainactivity extends Activity  implements  Sensoreventlistener {    PrivateTextView mtvshow;PrivateSensormanager Msensormanager;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Mtvshow = (TextView) Findviewbyid (r.id.tv_show);//Get Sensor Manager ObjectMsensormanager = (Sensormanager) getsystemservice (Context.sensor_service);//Get acceleration Sensor objectSensor sensor = Msensormanager.getdefaultsensor (Sensor.type_accelerometer);//Add listenerMsensormanager.registerlistener ( This, sensor, SENSORMANAGER.SENSOR_DELAY_UI); }@Override     Public void onsensorchanged(Sensorevent event) {//data returned by sensor        float[] values = event.values; StringBuffer buffer =NewStringBuffer (); Buffer.append ("The acceleration in the x direction is:"). Append (values[0]). Append ("\ n"); Buffer.append ("The acceleration in the y direction is:"). Append (values[1]). Append ("\ n"); Buffer.append ("The acceleration in the z direction is:"). Append (values[2]). Append ("\ n");    Mtvshow.settext (buffer); }@Override     Public void onaccuracychanged(Sensor sensor,intaccuracy) {}}
XML Page Layout
<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:paddingbottom="@dimen/activity_vertical_margin"    Android:paddingleft="@dimen/activity_horizontal_margin"    Android:paddingright="@dimen/activity_horizontal_margin"    Android:paddingtop="@dimen/activity_vertical_margin"    Tools:context=". Mainactivity ">    <textview  android:id  = "@+id/title"  android:layout_width  =< Span class= "Hljs-value" > "match_parent"  android:layout_height  = "wrap_content"  android:gravity  =" center " android:padding  =" 10DP "  android:text  = "acceleration Sensor"  android:textsize  = "20DP" />     <TextViewandroid:id= "@+id/tv_show"android:layout_width="Match_ Parent "android:layout_height="match_parent "android:layout_below=" @+id/ Title "android:textsize=" 18DP " />                                        </relativelayout>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. All articles, any technical point, whether original or reproduced, are used in the program or demo test before publishing! Bo Master qq:4878802.

Android Acceleration Sensor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.