Android phone shakes the implementation Sensoreventlistener See example
Package Com.example.shakeactivity;import Android.content.context;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.hardware.SensorManager; public class Shakelistener implements Sensoreventlistener {/* Sensor description * sensor.type_accelerometer Acceleration Sensing Test * SENSOR.TYPE_ Magnetic_field magnetic Field Sensing * sensor.type_orientation Azimuth sensing detection * Sensor.type_gyroscope gyroscope inductive detection * sensor.type_light brightness sensing detection * Sensor.type_pressure Pressure Sensing detection * sensor.type_temperature temperature Sensing detection * sensor.type_proximity proximity sensing detection * sensor_delay_fastest minimum delay. Generally not particularly sensitive handling is not recommended, this mode may cause a large consumption of mobile phone power. Because the original data is passed, the algorithm does not handle the performance of the game logic and UI, so it is not recommended. * Sensor_delay_game Game delay, most of the most real-time high-class games use this level * Sensor_delay_normal standard delay, for the general puzzle or easy-level games can be used, However, the low sample rate may have a jump frame phenomenon in some racing games.
* SENSOR_DELAY_UI user interface delay. Generally for the screen direction of their own active rotation use, relatively save energy and logic processing, general game development we do not use. */private static final int force_threshold = 350;private static final int time_threshold = 100;private static final int SH Ake_timeout = 500;private static final int shake_duration = 1000;private static final int shake_count = 6;private Sensorma Nager msensormgr;private Float mlastx = -1.0f, mlasty = -1.0f, Mlastz = -1.0f;private long mlasttime;private OnShakeListen ER mshakelistener;private Context mcontext;private int mshakecount = 0;private long mlastshake;private long MLastForce;pu Blic interface Onshakelistener {public void Onshake ();//public void Onshakehorizontal ();//public void Onshakevertical () ;} Public Shakelistener (Context context) {Mcontext = Context;resume ();} public void Setonshakelistener (Onshakelistener listener) {Mshakelistener = listener;} public void Resume () {msensormgr = (Sensormanager) mcontext.getsystemservice (Context.sensor_service); if (msensormgr = = NULL) {throw new Unsupportedoperationexception ("SensoRS not supported ");} Boolean supported = Msensormgr.registerlistener (this, msensormgr.getdefaultsensor (Sensor.type_accelerometer), SENSORMANAGER.SENSOR_DELAY_UI); if (!supported) {Msensormgr.unregisterlistener (this); throw new Unsupportedoperationexception ("Accelerometer not Supported");}} public void Pause () {if (msensormgr! = null) {Msensormgr.unregisterlistener (this); msensormgr = null;}} @Overridepublic void onaccuracychanged (sensor sensor, int accuracy) {} @SuppressWarnings ("deprecation") @ overridepublic void Onsensorchanged (Sensorevent event) {if (Event.sensor.getType ()! = Sensor.type_accelerometer) { return;} Long now = System.currenttimemillis (); if ((Now-mlastforce) > shake_timeout) {mshakecount = 0;} if ((now-mlasttime) > Time_threshold) {long diff = now-mlasttime;float speed = Math.Abs (Event.values[sensormanager. data_x]+ event.values[sensormanager.data_y]+ Event.values[sensormanager.data_z]-Mlastx-mlasty-mlastz)/diff * 10000 if (Speed > Force_threshold) {if (++mshaKecount >= Shake_count) && (Now-mlastshake > Shake_duration)) {mlastshake = Now;mshakecount = 0;if (MShakeL Istener! = null) {Mshakelistener.onshake ();}} Mlastforce = Now;} Mlasttime = Now;mlastx = Event.values[sensormanager.data_x];mlasty = Event.values[sensormanager.data_y];mlastz = Event.values[sensormanager.data_z];}}}
Save the above content as a Shakeactivity.java file, use the following method in Mainactivity.java
Shakelistener mshaker = new Shakelistener (this); Mshaker.setonshakelistener (new Shakelistener.onshakelistener () { public void Onshake () {//action while Shakingsettextview ("Shaked");});
Android Phone shakes the realization Sensoreventlistener