Introduction:
Today, in a bug that changes the alarm clock, we found that there is a shake behavior setting in the clock (Android 5.0) setting, mainly when the alarm rings, if we do not want to get up, you can set the shaking of the phone to turn off the ringtone or delay how long before the alarm.
Main principles
(1) Start a service and monitor the action of shaking the phone in the service.
(2) In the service, register a Sensoreventlistener monitoring event, processing sensor.type_accelerometer.
When shaking the phone, trigger the action of the event.
The triggering value of this trigger-shake event is sensitivity, which we can adjust to regulate the sensitivity of the trigger-shaking event.
Trigger to shake the phone after the event, do the corresponding action handleshakeaction (), I just pop up a toase, you can write your own want to do the task operation.
Core code (1) shakeservice-– Monitor Shake phone service
PackageCom.example.shakefunction;ImportAndroid.app.Service;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.hardware.Sensor;ImportAndroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.IBinder;ImportAndroid.util.Log;ImportAndroid.widget.Toast; Public class shakeservice extends Service{ Public Static FinalString TAG ="Shakeservice";PrivateSensormanager Msensormanager;@Override Public void onCreate() {//TODO auto-generated method stub Super. OnCreate (); Msensormanager = (Sensormanager) getsystemservice (Context.sensor_service); }@Override Public void OnDestroy() {//TODO auto-generated method stub Super. OnDestroy (); Msensormanager.unregisterlistener (Mshakelistener); }@Override Public void OnStart(Intent Intent,intStartid) {//TODO auto-generated method stub Super. OnStart (Intent, Startid); }@Override Public int Onstartcommand(Intent Intent,intFlagsintStartid) {//TODO auto-generated method stubMsensormanager.registerlistener (Mshakelistener, Msensormanager.getdefaultsensor (Sensor.TYPE_ACCELEROMETER),//sensormanager.sensor_delay_game, -* +);//batch every milliseconds return Super. Onstartcommand (Intent, flags, Startid); }Private FinalSensoreventlistener Mshakelistener =NewSensoreventlistener () {Private Static Final floatSensitivity = -;Private Static Final intBUFFER =5;Private float[] Gravity =New float[3];Private floatAverage =0;Private intFill =0;@Override Public void onaccuracychanged(Sensor sensor,intACC) {} Public void onsensorchanged(Sensorevent event) {Final floatAlpha =0.8F for(inti =0; I <3; i++) {Gravity[i] = Alpha * Gravity[i] + (1-Alpha) * Event.values[i]; }floatx = event.values[0]-gravity[0];floaty = event.values[1]-gravity[1];floatz = event.values[2]-gravity[2];if(Fill <= BUFFER) {average + = Math.Abs (x) + Math.Abs (y) + Math.Abs (z); fill++; }Else{LOG.I (TAG,"Average:"+average); LOG.I (TAG,"Average/buffer:"+ (Average/buffer));if(Average/buffer >= sensitivity) {handleshakeaction (); } average =0; Fill =0; } } };protected void handleshakeaction() {//TODO auto-generated method stubToast.maketext (Getapplicationcontext (),"Shake Success", Toast.length_long). Show (); }@Override PublicIBinderOnbind(Intent Intent) {//TODO auto-generated method stub return NULL; }}
(2) Start and close Shakeservice in the main interface
new Intent(); intent.setClass(this, ShakeService.classif(view == startService){ startService(intent); }elseif(view == stopService){ stopService(intent); }
(3) in Androidmanifest.xml definition Shakeservice
<service android:name=".ShakeService" android:exported="false"></service>
SOURCE download
http://download.csdn.net/detail/hfreeman2008/8970759
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android shake-shake function implementation