Android Acceleration Sensor

Source: Internet
Author: User

Android Acceleration Sensor
Android Acceleration Sensor

Two screenshots of the mobile phone's desktop are flushed.


Source code

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

Procedure

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

Code
Package com. example. kongqw. kqwsensorforaccelerometerdemo; import android. app. activity; import android. content. context; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. textView; public class MainActivity extends Activity implements SensorEventListener {private TextView mTvShow; private SensorManager mSensorManager; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTvShow = (TextView) findViewById (R. id. TV _show); // get the sensor manager object mSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); // obtains the Sensor Object sensor Sensor = mSensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER); // Add the listener mSensorManager. registerListener (this, sensor, SensorManager. SENSOR_DELAY_UI) ;}@ Override public void onSensorChanged (SensorEvent event) {// The data returned by the sensor float [] values = event. values; StringBuffer buffer = new StringBuffer (); buffer. append (the acceleration in the X direction is :). append (values [0]). append (); buffer. append (the acceleration in Y direction is :). append (values [1]). append (); buffer. append (the acceleration in the Z direction is :). append (values [2]). append (); mTvShow. setText (buffer) ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy ){}}
XML Page Layout

      
       
    
   
  

 

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.