6

How to control a music player from buttons in notification, and how to listen button action from the notification

Community
  • 1
  • 1
Babin
  • 70
  • 1
  • 1
  • 9
  • I have no idea about that, but i need to use remote view for this. pls post any sample code for this – Babin Feb 19 '14 at 05:49
  • Follw Below link and you will get proper answer. [Full Code][1] [1]: http://stackoverflow.com/questions/12526228/how-to-put-media-controller-button-on-notification-bar/25379408#25379408 – Roadies Aug 19 '14 at 09:07

1 Answers1

10

You should use setOnClickPendingIntent() of RemoteViews to listen button action from the notification.

The method setOnClickPendingIntent() will bind a PendingIntent to response the button click event.

The sample code is like this:

private void showControllerInNotification() {       
    PendingIntent pendingIntent = null;
    Intent intent = null;


    //Inflate a remote view with a layout which you want to display in the notification bar.
    if (mRemoteViews == null) {
        mRemoteViews = new RemoteViews(getPackageName(),
                R.layout.notification_control_bar);
    }   

    //Define what you want to do after clicked the button in notification.
    //Here we launcher a service by an action named "ACTION_STOP" which will stop the music play.
    intent = new Intent(ACTION_STOP);       
    pendingIntent = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_STOP, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    //In R.layout.notification_control_bar,there is a button view identified by bar_btn_stop
    //We bind a pendingIntent with this button view so when user click the button,it will excute the intent action.
    mRemoteViews.setOnClickPendingIntent(R.id.bar_btn_stop,
            pendingIntent);

    //Create the notification instance.
    mNotification = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.ic_launcher).setOngoing(true)
            .setWhen(System.currentTimeMillis())                
            .setContent(mRemoteViews)
            .build();

    //Show the notification in the notification bar.
    mNotifiManager.notify(NOTIFICATION_ID, mNotification);      
}   

update 1:

The service which responses the action is like this:

public class MediaPlayerService extends Service {

    public static final String ACTION_STOP="xxx.yyy.zzz.ACTION_STOP";
    public static final String ACTION_PLAY="xxx.yyy.zzz.ACTION_PLAY";
    public static final String ACTION_PAUSE="xxx.yyy.zzz.ACTION_PAUSE";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();         
        if (!TextUtils.isEmpty(action)) {
            if (action.equals(ACTION_PLAY)) {
                startPlay();
            }else if(action.equals(ACTION_PAUSE)) {
                pausePlay();
            }else if(action.equals(ACTION_STOP)) {
                stopPlay();
            }
        }
    }
    return super.onStartCommand(intent, flags, startId);
}
private void stopPlay(){
    // do the play work here
}
private void stopPause(){
    // do the pause work here
}
private void stopPlay(){
    // do the stop work here
}

}

Register the service in manifest:

    <service
        android:name="xxx.yyy.zzz.MusicPlayService"
        android:exported="false" >
        <intent-filter>
            <action android:name="xxx.yyy.zzz.ACTION_PLAY" />
            <action android:name="xxx.yyy.zzz.ACTION_PAUSE" />
            <action android:name="xxx.yyy.zzz.ACTION_STOP" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

For the music play control,you can copy some useful code sinppets from here.

abhijit.mitkar
  • 246
  • 2
  • 7
Riki
  • 2,774
  • 1
  • 24
  • 38
  • @AvatarQing gives me a suggestion. I build a notification big view using RemoteView to control play/pause like this link (stackoverflow.com/questions/14508369/…) All are right but when i click device back button and out from the application click event(Play/Pause/Forward/Close) button doesn't work.Please help me. – Helal Khan Mar 11 '14 at 14:05
  • Did you do the play/pause controls in Activity,not in Service? – Riki Mar 17 '14 at 03:44
  • can you explain the intent filter in the service please? – juztcode Sep 11 '20 at 04:46
  • 1
    @juztcode those are your custom actions you could define – Riki Sep 11 '20 at 16:38
  • @AvatarQing , these are optional or mandatory? I've followed tutorials and these weren't mentioned – juztcode Sep 12 '20 at 03:48
  • @juztcode the action is correspond to the intent that you pass to PendingIntent.getService() , whether it's necessary or not depends on your need. – Riki Sep 13 '20 at 07:48