Short press the headset key to pause/play the FM Function

Source: Internet
Author: User

By default, the short press headset key is used to pause/play music. You can press the headset key to pause/play FM as follows:

 

 
Please delete your media button code, modify as below:

1. AndroidMenifest. xml:

<Cycler android: name = ". FMRadioActivity $ MediaButtonReceiver">

<Intent-filter>

<Action android: name = "android. intent. action. MEDIA_BUTTON"/>

</Intent-filter>

</Cycler>

 

2. FMRadioActivity. java code:

 

Public interface MediaButtonListener {

Void onMediaButtonCall (Intent intent );

}

Private MediaButtonListener mMediaButtonListener = new MediaButtonListener (){

 

@ Override

Public void onMediaButtonCall (Intent intent ){

String action = intent. getAction ();

LogUtils. d (TAG, "onMediaButtonCall. action:" + action );

KeyEvent event = (KeyEvent)

Intent. getParcelableExtra (Intent. EXTRA_KEY_EVENT );

LogUtils. d (TAG, "onMediaButtonCall. event:" + event );

If (Intent. ACTION_MEDIA_BUTTON.equals (action )){

LogUtils. d (TAG, "-- Mediareceiver. event. getKeyCode ():" + event. getKeyCode ());

LogUtils. d (TAG, "-- Mediareceiver. event. getAction ():" + event. getAction ());

If (KeyEvent. KEYCODE_MEDIA_PLAY_PAUSE = event. getKeyCode () & event. getAction () = KeyEvent. ACTION_UP ){

LogUtils. d (TAG, "-- Mediareceiver. pause ");

If (mIsPlaying ){

LogUtils. d (TAG, "onMediaButtonCall. power down fm ");

RefreshImageButton (false); refreshActionMenuItem (false );

RefreshPopupMenuItem (false );

RefreshActionMenuPower (false); exitService ();

}

} Else if (KeyEvent. KEYCODE_MEDIA_NEXT = event. getKeyCode () & event. getAction () = KeyEvent. ACTION_UP ){

If (mIsPlaying ){

LogUtils. d (TAG, "onMediaButtonCall. seek next station ");

SeekStation (mCurrentStation, true );

}

}

}

}

};

Public static class MediaButtonReceiver extends BroadcastReceiver {

Static MediaButtonListener mListener = null;

@ Override

Public void onReceive (Context context, Intent intent ){

String action = intent. getAction ();

If (null! = MListener ){

MListener. onMediaButtonCall (intent );

This. abortBroadcast ();

}

}

Public static void registerMediaButtonListener (MediaButtonListener callback ){

MListener = callback;

}

}

 

OnCreate method:

MediaButtonReceiver. registerMediaButtonListener (mMediaButtonListener );

ComponentName rec = new ComponentName (getPackageName (),

MediaButtonReceiver. class. getName ());

MAudioManager. registerMediaButtonEventReceiver (rec );

 

PowerupFM method:

MAudioManager. registerMediaButtonEventReceiver (new ComponentName (this. getPackageName (),

MediaButtonReceiver. class. getName ()));

 

ExitService method:

MAudioManager. unregisterMediaButtonEventReceiver (new ComponentName (this. getPackageName (),

MediaButtonReceiver. class. getName ()));

 

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.