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