On an article on how to match lyrics online, http://blog.csdn.net/huweigoodboy/article/details/39878063, now speaking of shaking a shake function development.
Also used a service to shake it.
Shakelistener inherits from Sensoreventlistener, and when the accelerometer senses a gravitational change, it notifies Onshakelistener to call Onshake (),
Random a song at the same time as shaking.
One, acceleration sensor
Here you need to set a speed threshold and time interval, control a certain time interval to trigger a second vibration, and feel the speed is too small to go onshake ().
1) Get Sensormanager.
Sensormanager= (Sensormanager) Mcontext.getsystemservice (Context.sensor_service);
2) Get sensor
Sensor=sensormanager.getdefaultsensor (Sensor.type_accelerometer);
3) Registered Sensing monitor
Sensormanager.registerlistener (this, sensor, sensormanager.sensor_delay_normal);
Package Com.huwei.sweetmusicplayer.ui.listeners;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 {//Speed threshold private static final int speed_shakehold=3000;// Detection interval private static final int uptate_interval_time = 70;//sensor Manager Private Sensormanager sensormanager;//sensor private Sensor sensor;//gravity sensor listener Private Onshakelistener onshakelistener;//Context Private Context mcontext;// Cell phone previous position Gravity sensing coordinates private float lastx;private float lasty;private float lastz;//Last detection time private long lastupdatetime;public Shakelistener (Context mcontext) {super (); this.mcontext = Mcontext;start ();} public void Start () {sensormanager= (Sensormanager) Mcontext.getsystemservice (Context.sensor_service); Sensormanager!=null) {//Get acceleration sensor sensor=sensormanager.getdefaultsensor (sensor.type_accelerometer);} if (sensor!=null) {//Register accelerator, and set rate Sensormanager.registerListener (this, sensor, sensormanager.sensor_delay_normal);}} Set the gravity sense listener public void Setonshakelistener (Onshakelistener listener) {Onshakelistener = listener;} public void Stop () {Sensormanager.unregisterlistener (this);} Gravity sensing to change @overridepublic void Onsensorchanged (Sensorevent event) {//TODO auto-generated method Stublong Currentupdatetime=system.currenttimemillis (); Long Timeinterval=currentupdatetime-lastupdatetime;if (timeInterval <uptate_interval_time) {return;} Lastupdatetime=currentupdatetime;float x=event.values[0];float y=event.values[1];float Z=event.values[2];float Deltax=x-lastx;float deltay=y-lasty;float deltaz=z-lastz;lastx=x;lasty=y;lastz=z;double speed=Math.sqrt (deltaX* Deltax+deltay*deltay*deltaz*deltaz)/timeinterval*10000;if (speed>speed_shakehold) {onShakeListener.onShake ();}} @Overridepublic void onaccuracychanged (sensor sensor, int accuracy) {//TODO auto-generated method stub}//Shake listener interface public in Terface Onshakelistener {public void Onshake ();}}
two, vibration control
Get Vibrator Object
Vibrator= (Vibrator) Getbasecontext (). Getsystemservice (Context.vibrator_service);
There are two methods of vibration
Vibrator.vibrate (500); how many milliseconds to shake?
or set an array of vibration periods
Vibrator.vibrate (New long[]{100,200,100,200},-1); //Stop, vibrate, stop, shake
1 = Repeat count is not duplicated
Package Com.huwei.sweetmusicplayer.services;import Com.huwei.sweetmusicplayer.datamanager.musicmanager;import Com.huwei.sweetmusicplayer.ui.listeners.ShakeListener; Import Android.app.service;import android.content.context;import android.content.intent;import android.os.IBinder; Import Android.os.vibrator;import Android.util.log;public class Shakeservice extends Service {private static final String tag= "Shakeservice";p rivate shakelistener mshakelistener;private vibrator vibrator; @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); vibrator= (vibrator) Getbasecontext (). Getsystemservice (Context.vibrator_service); Mshakelistener=new Shakelistener (Getbasecontext ()); Mshakelistener.setonshakelistener (New Shakelistener.onshakelistener () {@Overridepublic void Onshake () {//TODO auto-generated method stub Mshakelistener.stop (); Startvibrator (); Vibrator.cancel (); Mshakelistener.start ();}} );} vibration function void Startvibrator () {log.i (TAG, "shake"); Vibrator.vibrate (500); MusicmAnager.getinstance (). Random_a_song ();} @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {//TODO auto-generated method Stubmshakelistene R.start (); return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); Mshakelistener.stop ();}}
Android music player Development Sweetmusicplayer Shake and change song