play/pause button image in notification ,Android
Asked Answered
M

4

15

I have implemented music player which fires a custom notification when stream audio is playing.

Everything is working and I can play/pause the audio using the button in the notification. The only problem is the image button: it can't change the image on a click of a button to indicate play/pause.

Using remoteViews.setImageViewResource() in RemoteReceiver is not working. The control is done using BroadcastReceiver and this is the code of firing the notification from the player activity:

  public void setNotification(String songName){
      String ns = Context.NOTIFICATION_SERVICE;
      NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

      @SuppressWarnings("deprecation")
      Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());

      RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_view);
      notificationView.setImageViewResource(R.id.button1, R.drawable.pause);
      notificationView.setTextViewText(R.id.textView1, songName);

      //the intent that is started when the notification is clicked (works)
      Intent notificationIntent = new Intent(this, PlayerActivity.class);
      PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

      notification.contentView = notificationView;
      notification.contentIntent = pendingNotificationIntent;     

      Intent switchIntent = new Intent("com.example.test.ACTION_PLAY");
      PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

      notificationView.setOnClickPendingIntent(R.id.play_pause, pendingSwitchIntent);
      notificationManager.notify(1, notification);
  }

The notification.xml content is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <ImageView
        android:layout_alignParentLeft="true"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher"
        android:id="@+id/imgAppIc" />



    <TextView
        android:layout_toRightOf="@id/imgAppIc"
        android:singleLine="true"
        android:id="@+id/textView1"
        android:ellipsize="marquee"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="10dp"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:layout_width="170dp"
        android:layout_height="wrap_content"/>


    <ImageButton
        android:id="@+id/play_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/play"

         />

</RelativeLayout>

this is the RemoteRecivier class

public class RemoteControlReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.notification_view);    

        if(action.equalsIgnoreCase("com.example.test.ACTION_PLAY")){
            if(mediaplayer.isPlaying()){
                mediaplayer.pause();

                remoteViews.setImageViewResource(R.id.button1, R.drawable.play);
            }
            else {
                mediaplayer.start();
                remoteViews.setImageViewResource(R.id.button1, R.drawable.pause);
            }
        }
    }
}

and finally, the manifest is

 <receiver android:name=".RemoteControlReceiver">
            <intent-filter>
                <action android:name="com.Music.app.ACTION_PLAY" />
            </intent-filter>


        </receiver>

<activity android:name="PlayerActivity" />
Mook answered 26/6, 2014 at 17:44 Comment(0)
L
6

You have to set your notification.contentView to the new remoteView after it was changed so it can update the notification view itself.

Meaning, after you receive the action in your BroadcastReceiver, re-build your notification with the desired button display ( pause or play )

Hope this helps

Looseleaf answered 29/6, 2014 at 5:45 Comment(6)
Doing that, won't the notification twinkle ?Jabez
Twinkle would you post the answer( with code ) here ? Please we are trying this for days .. After setting notification.contentView = new remoteView() The notification button still doesn't work.Astound
@OmkarJadhav it worked as AlAsiri said . after did the change and change the button image , re- notify notificationManager.notify(1, notification); this will show the update . and insure to use the same id which is here number 1.. hope you get it now and tell me if u need helpMook
But won't this be a new notification?Macfarlane
@Macfarlane use the same id to avoid this problemPeridium
Not for me : Bad notification posted from package com.nikola.starter: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.nikola.starter user=UserHandle{0} id=1 tag=null score=20: Notification(pri=2 contentView=com.nikola.starter/0x7f040025 vibrate=default sound=default defaults=0xffffffff flags=0x1 kind=[null]))Bocage
A
4

This should work. Just make your notification and notificationManager a static global variable.

public class RemoteControlReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {  

        if(action.equalsIgnoreCase("com.example.test.ACTION_PLAY")){
            if(mediaplayer.isPlaying()){
                mediaplayer.pause();
                notificationView.setImageViewResource(R.id.button1, R.drawable.play);
                notification.contentView = notificationView;
                notificationManager.notify(1, notification);
            }
            else {
                mediaplayer.start();
                notificationView.setImageViewResource(R.id.button1, R.drawable.pause);
                notification.contentView = notificationView;
                notificationManager.notify(1, notification);
            }
        }
    }
}
Assembler answered 17/3, 2015 at 14:10 Comment(1)
Something strange here . i have same staff but not works . How can you access notificationView like that ? Service1.mNotificationManager.notify(1, Service1.mBuilder.build()); this line makes app to crack ...Bocage
A
-1

Hey guys you only need to update "notificationManager.notify(1, notification);" in your onReceiver method after you make changes to same notification. Assign public static to your notification and notificationManager and use in your BroadcastReceiver extended class.

See the below code to know accurately :


 public void onReceive(Context context, Intent intent) {


    String action1 = intent.getStringExtra("action1");
       // String action2 = intent.getStringExtra("action2");

        Log.i("Intent Received","Yes");


    if(say)
    {
        if (action1.equals("PauseAction"))
        {
            performAction1();
            say = false;

        }
    }

    else if(!say)
    {

        if (action1.equals("PauseAction"))
        {
            performAction2();
            say = true;


        }
    }

   public void performAction1()
    {

   if (mediaPlayer!=null)
 {

     if(mediaPlayer.isPlaying())
        {
            playButtonImage.setImageResource(R.drawable.playbutton);
            playButtonImage.setContentDescription("Play Button ");


 notificationView.setImageViewResource(R.id.remoteViewImageView,R.drawable.playbutton);
            mediaPlayer.pause();
            play = true;

            MainActivity.notificationManager.notify(1, notification);


        }

        Log.i("performAction1 called ","Yes");

    }
  }


    public void performAction2()
    {

        if (mediaPlayer != null)
        {
            mediaPlayer.start();
            playButtonImage.setImageResource(R.drawable.pause);
            playButtonImage.setContentDescription("Pause Button");
            notificationView.setImageViewResource(R.id.remoteViewImageView,R.drawable.pause);
            MainActivity.notificationManager.notify(1, notification);

            play = false;

            Log.i("performAction2 called ", "Yes");
        }
    }
}

Andri answered 9/9, 2019 at 7:13 Comment(0)
F
-2

Why not using an ImageView instead of ImageButton? With ImageView, you can simply change its appearance with removeViews.setImageViewResource(R.id.button1, R.drawable.pause);

Firecracker answered 23/12, 2014 at 8:14 Comment(2)
This doesn't come to my mind .. I never tried this before , may I'll next time .Mook
@Jason It does not matter its a Imageview or ImageButton. setImageViewResource() do the same work for both of them.Assembler

© 2022 - 2024 — McMap. All rights reserved.