-1

Currently I have a notification panel which has three buttons..one for opening the activity, one for pause upload and the other for resume upload.

I want the pause upload and resume upload to have a single button like the pause/play button in Google Music Player.

I have used this answer for building notification panel. Please suggest!

Notification Panel Class:

public NotificationPanel(Context parent) {
// TODO Auto-generated constructor stub
this.parent = parent;
nBuilder = new NotificationCompat.Builder(parent)
.setContentTitle("Notification Title")
.setSmallIcon(R.drawable.logo)
.setOngoing(true);

remoteView = new RemoteViews(parent.getPackageName(), R.layout.notification_layout);   


//set the button listeners  

setListeners(remoteView);
nBuilder.setContent(remoteView);

nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(2, nBuilder.build());
}

public void setListeners(RemoteViews view){
Intent stopNotify = new Intent(parent,HelperActivity.class);
stopNotify.putExtra("DO", "stop");
PendingIntent btn1 = PendingIntent.getActivity(parent, 0, stopNotify, 0);
view.setOnClickPendingIntent(R.id.notifyStopButton, btn1); 

Intent pauseUpload = new Intent(parent,HelperActivity.class);
pauseUpload.putExtra("DO", "pause");    
PendingIntent btn2 = PendingIntent.getActivity(parent, 1, pauseUpload, 0);
view.setOnClickPendingIntent(R.id.uploadPauseButton, btn2); 

Intent resumeUpload = new Intent(parent,HelperActivity.class);
resumeUpload.putExtra("DO", "upload");  
PendingIntent btn3 = PendingIntent.getActivity(parent, 2, resumeUpload, 0);
view.setOnClickPendingIntent(R.id.uploadResumeButton, btn3); 

}

public void notificationCancel() {
     nManager.cancel(2);
}
Community
  • 1
  • 1
Jaswanth Kumar
  • 3,531
  • 3
  • 23
  • 26

2 Answers2

2

You should use a counter that determine the status of the button. For example let's say:

Status1= pause upload methods , pause upload image -Event click- Status2= resume upload methods , resume upload image

Both status are on a single button

Deket
  • 49
  • 7
  • How can I change the image resource simultaneously? I tried view.setImageViewResource(viewId, srcId) but it changes only one time. – Jaswanth Kumar Dec 05 '14 at 07:46
0

I am aware that this is an older question, but seeing I was also figuring this out, I thought I'd offer my solution. The trick for me was finding out when to update the button. It turned out to be crucial to do this only after pausing the player, so I could use the isPlaying boolean for both handling the playback and the switching image. I do not use the listener approach, as I do not want to open the app when just controlling the playback. Here is how it works for me:

class NotificationPanel {

    private static RemoteViews remoteView;
    private final Context ctxt;
    private final String title;
    private static NotificationManager nManager;
    private static Notification.Builder nBuilder;

    public NotificationPanel(Context ctxt, String title) {
        this.ctxt = ctxt;
        this.title = title;

        createNotification();
    }

    private void createNotification() {
        //Set up:
        // – remoteView and Notifciation
        // – notification manager and notification builder

        updateButton();
        updateNotification();
    }

    public static void updateButton() {
        int image;
        if (MainActivity.isPlaying) {// boolean registering if media is player
            image = android.R.drawable.ic_media_pause;
        } else {
            image =  android.R.drawable.ic_media_play;
        }
        remoteView.setImageViewResource(R.id.play_pause_bt, image);
    }

    public static void updateNotification() {
        nBuilder.setContent(remoteView);
        nManager.notify(Constants.NOTIFICATION_ID, nBuilder.build());
    }

    public static class NotificationReceiver extends BroadcastReceiver {

        private final MainActivity mainActivity;
        private IntentFilter intentFilter;

        public NotificationReceiver(MainActivity mainActivity) {
            this.mainActivity = mainActivity;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String action = intent.getAction();
                assert action != null;
                switch (action) {
                    case Constants.OPEN_APP:
                        break;
                    case Constants.ACTION_PAUSE_PLAY:
                        if (MainActivity.isPlaying) {
                            mainActivity.pausePlayback();
                        } else {
                            mainActivity.startPlayback();
                        }
                        updateButton();
                        updateNotification();
                }
            }
        }

        //setup intent filters
    }
}
atschpe
  • 121
  • 14