2

I'm trying to implement play/pause button in notification via frame layout but when I click to pause the mediaPlayer it doesn't updates to show the play button in the notification.How to update the view?

RemoteViews nv = new RemoteViews(getPackageName(), R.layout.notificationLayout);
 Intent switchIntent = new Intent("com.example.android.ACTION_PLAY");
 PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, 0);
 nv.setOnClickPendingIntent(R.id.pause, pendingSwitchIntent);

In Broadcast Receiver class

@Override

public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if(action.equalsIgnoreCase("com.example.app.ACTION_PLAY")){
                if(mp.isPlaying())
                {
                    nv.setViewVisibility(R.id.pause, View.INVISIBLE);
                    nv.setViewVisibility(R.id.play, View.VISIBLE);
                    mp.pause();
                }
                else
                {
                    nv.setViewVisibility(R.id.play, View.INVISIBLE);
                    nv.setViewVisibility(R.id.pause, View.VISIBLE);
                    mp.start();
                }

            }
        }  
zek54
  • 415
  • 3
  • 20

2 Answers2

0

Try it!!!!

@Override

public void onReceive(Context context, Intent intent) {
Notification notification = null;
        String action = intent.getAction();

        if(action.equalsIgnoreCase("com.example.app.ACTION_PLAY")){
            if(mp.isPlaying())
            {
                notification.contentView.setViewVisibility(R.id.pause, View.INVISIBLE);
                notification.contentView.setViewVisibility(R.id.play, View.VISIBLE);
                mp.pause();
            }
            else
            {
                notification.contentView.setViewVisibility(R.id.play, View.INVISIBLE);
                notification.contentView.setViewVisibility(R.id.pause, View.VISIBLE);
                mp.start();
            }

        }
    }  
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22
  • It doesn't work.Is there a way to get the same notification object in the broadcast receiver class? – zek54 Apr 13 '16 at 06:22
0

You could post a new notification with another icon to foreground again to refresh the remote views.

RemoteViews can be reused, but startForeground(NOTIFICATION_ID, mNotification); should be called after you change the content of RemoteView.


I think it is because notification communicate with system notification via Parcel , the content has been clone to a new instance.

sakiM
  • 4,832
  • 5
  • 27
  • 33
  • How to get the notification object in receiver class? – zek54 Apr 13 '16 at 06:29
  • If the `Receiver` is your inner class, you can access it easily. Or you can set a callback to your media service, to post a new notification with new play state. – sakiM Apr 13 '16 at 06:31
  • There is a common foreground notification guide [here](http://stackoverflow.com/questions/6397754/android-implementing-startforeground-for-a-service). In this way, you can save your `notification` to anywhere you like. – sakiM Apr 13 '16 at 06:37