Android Platform Shake song function

Source: Internet
Author: User

A few days ago, Indonesia customers asked to add a rocker-cut song feature on Qualcomm platform 7251. Check some information, the basic realization of this function.


Directly on the source, use SVN to view the modification point.

The first two ic_mp_shake_off_btn are two pictures, the resources to let the UI do two.

The next step is to modify the Audio_player.xml file: (This is where you want to place your icon button)

Corresponding to modify the layout file of other resolutions Audio_player.xml

Next add the corresponding string: String.xml

<span style= "FONT-SIZE:24PX;" >    <string name= "Shake_on_notif" > Shake to Change Song function has been turned on .</string>    <string name= "Shake_off_notif" > Shake to change song function closed .</string></span>
the translation of national languages is also added themselves.

Next change the main class of music playback: Mediaplaybackactivity.java

The first thing to add is the variable: private ImageButton Mshakebutton;

Next, we use Findviewbyid to instantiate the Shake button and add monitoring;

<pre name= "code" class= "java" ><span style= "FONT-SIZE:24PX;" >    Mshakebutton = ((ImageButton) Findviewbyid (R.id.shake));    Mshakebutton.setonclicklistener (Mshakelistener); </span>

The same operation is done in the function onconfigurationchanged;

<span style= "FONT-SIZE:24PX;" >    Mshakebutton = ((ImageButton) Findviewbyid (R.id.shake));    Mshakebutton.setonclicklistener (Mshakelistener); </span>
now to implement the function of listening;

<span style= "FONT-SIZE:24PX;" >    <span style= "FONT-SIZE:18PX;" >private View.onclicklistener Mshakelistener = new View.onclicklistener () {public        void OnClick (View v) {            Shakeenable ();        }    }; </span></span>

Write the function body of the shakeenable () function;

<span style= "FONT-SIZE:24PX;" >    <span style= "FONT-SIZE:18PX;" >private void Shakeenable () {if (Mservice = = null) {return;} try {if (mservice.getshakeflag () = = False) {Mservice.setshakeflag (true); Showtoast (r.string.shake_on_notif);} Else{mservice.setshakeflag (false); Showtoast (r.string.shake_off_notif);} Setshakebuttonimage ();} catch (RemoteException ex) {        }    }</span></span>

In the serviceconnection to modify the operation of the button and set the picture;

Write Setshakebuttonimage function:

<span style= "FONT-SIZE:24PX;" >   <span style= "FONT-SIZE:18PX;" >private void Setshakebuttonimage () {        if (Mservice = = null) return;          try {if  (mservice.getshakeflag () = = False) {Mshakebutton.setimageresource (R.DRAWABLE.IC_MP_SHAKE_OFF_BTN);} Else{mshakebutton.setimageresource (R.DRAWABLE.IC_MP_SHAKE_ON_BTN);}   } catch (RemoteException ex) {        }    }</span></span>

Mediaplaybackactivity.java This file has been modified;


Now look at Mediaplaybackservice.java.

Import Onshakelistener First

<span style= "FONT-SIZE:24PX;" >import com.android.music.shakedetector.onshakelistener;</span>

Declaration variables:

Private Boolean mshakeflag = false;

Shakedetector mshakedetector = null;

Read the value from the Sharedpreference in OnCreate () to set whether the Shake function is turned on;

<span style= "FONT-SIZE:24PX;" >  <span style= "FONT-SIZE:18PX;" >mshakeflag = Mpreferences.getboolean ("Shakeflag", false);  Setshakeonoroff ();</span></span>

To unregister in OnDestroy ():

<span style= "FONT-SIZE:24PX;" >   <span style= "FONT-SIZE:18PX;" >if (Mshakedetector! = null) {Mshakedetector.unregisterlistener ();   } </span></span>
implementation functions:

<span Style= "FONT-SIZE:24PX;" > <span style= "font-size:18px;" > private void Setshakeonoroff () {if (Mshakeflag = = True) {mshakedetector= new shakedetector (this); Mshakedetec Tor.registerlistener ();  Mshakedetector.setonshakelistener (New Onshakelistener () {@Override public void Onshake () {//TODO auto-generated method Stub if (isplaying ()) {GoToNext (true);}} }); }else{if (Mshakedetector! = null) {Mshakedetector.unregisterlistener ();}} Editor ed = Mpreferences.edit (); Ed.putboolean ("Shakeflag", Mshakeflag); Sharedpreferencescompat.apply (ed);            The public void Setshakeflag (Boolean shakeflag) {synchronized (this) {mshakeflag = Shakeflag;        Setshakeonoroff ();    }} public Boolean Getshakeflag () {return mshakeflag; }</span></span> 

Set whether to turn on the Shake function in Imediaplaybackservice.stub

<span style= "FONT-SIZE:24PX;" >      <span style= "FONT-SIZE:18PX;" >public void Setshakeflag (Boolean shakeflag) {    mservice.get (). Setshakeflag (Shakeflag);          }        public Boolean Getshakeflag () {            return Mservice.get (). Getshakeflag ();        } </span></span>

The next step is to add the corresponding function in the Imediaplaybackservice.aidl file;

<span style= "FONT-SIZE:24PX;" >   <span style= "FONT-SIZE:18PX;" >void Setshakeflag (Boolean shakeflag);    Boolean Getshakeflag ();</span></span>

The end is to add a file Shakedetector.java

<span style= "FONT-SIZE:18PX;" >package Com.android.music; Import Android.content.context;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.hardware.sensormanager;import Android.util.Log;public Class Shakedetector implements Sensoreventlistener {private static final String TAG = "Shakedetector";p rivate Context mcontext;        Private long lasttime; Private float last_x;private float last_y;private float last_z;private static final double shake_shreshold = 3000d;//This value can be rooted According to sensor sensitivity to adjust the private sensor sensor;private Sensormanager sensormanager;public Onshakelistener shakelistener;publi C Shakedetector (Context context) {Mcontext = Context;sensormanager = (sensormanager) context.getsystemservice (       Context.sensor_service);} public Boolean Registerlistener () {if (Sensormanager! = null) {sensor = Sensormanager.getdefaultsensor (sensor.type_ ACCELEROMETER); if (sensor! = null) {This.sensorManager.registerListeNER (this, sensor,sensormanager.sensor_delay_game); return true;}} return false;}  public void Unregisterlistener () {System.out.println ("Shakedetector:unregisterlistener"); if (Sensormanager! = null && sensor! = NULL) Sensormanager.unregisterlistener (this, sensor);} public void Setonshakelistener (Onshakelistener listener) {Shakelistener = listener;} @Overridepublic void onaccuracychanged (sensor sensor, int accuracy) {//TODO auto-generated method stub} @Overridepublic V OID onsensorchanged (Sensorevent event) {//TODO auto-generated method Stublong Curtime = Java.lang.System.currentTimeMillis (), if ((Curtime-lasttime) >) {Long difftime = (curtime-lasttime); lasttime = CU                                Rtime;float x = event.values[0];float y = event.values[1];float z = event.values[2]; Float speed = Math.Abs (x + y + z-last_x-last_y-last_z)/difftime * 10000; System.out.println ("shakedetector:onsensorchanged speed =" +speed), if (Speed > Shake_shreshold) { Shakelistener.onshake();}       last_x = x;last_y = Y;last_z = Z;}} Public interface Onshakelistener {public void Onshake ();}} </span>

OK, so far, you can achieve the shake-cut song function, if useful, you can take it, other platforms (Spreadtrum, MTK) can also be similar to the implementation of this function, there are problems please leave a message in the back.





Android Platform Shake song function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.