The main introduction to the Android Acceleration sensor simple usage, simulation shake, if the x, Y, z Three direction acceleration more than 15, will pop up toast, of course, you can set more complex strategies, such as the Judgment interval code as follows
Public class mainactivity extends Activity { PrivateSensormanager Sensormanager;PrivateTextView TextView;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service); Sensor sensor = Sensormanager.getdefaultsensor (Sensor.type_accelerometer); Sensormanager.registerlistener (Sensoreventlistener,sensor,sensormanager.sensor_delay_normal); }PrivateSensoreventlistener Sensoreventlistener =NewSensoreventlistener () {@Override Public void onsensorchanged(Sensorevent sensorevent) {floatXValue = Math.Abs (sensorevent.values[0]);floatYvalue = Math.Abs (sensorevent.values[1]);floatZvalue = Math.Abs (sensorevent.values[2]);if(XValue > the|| Yvalue > the|| Zvalue > the) {Toast.maketext (mainactivity. This,"Shake it.", Toast.length_long). Show (); } }@Override Public void onaccuracychanged(Sensor sensor,inti) {}};@Override protected void OnDestroy() {if(Sensormanager! =NULL) {Sensormanager.unregisterlistener (Sensoreventlistener); }Super. OnDestroy (); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.menu_main, menu);return true; }@Override Public Boolean onoptionsitemselected(MenuItem Item) {//Handle Action Bar item clicks here. The Action Bar would //automatically handle clicks on the Home/up button, so long //As you specify a parent activity in Androidmanifest.xml. intid = item.getitemid ();//noinspection simplifiableifstatement if(id = = r.id.action_settings) {return true; }return Super. onoptionsitemselected (item); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android acceleration sensor simulates shaking effect-android learning Journey (66)