音乐播放器就像通知面板中的控制按钮一样

时间:2014-12-05 07:34:45

标签: android android-notifications

目前我有一个通知面板,其中有三个按钮..一个用于打开活动,一个用于暂停上传,另一个用于恢复上传。

我希望暂停上传和恢复上传只有一个按钮,例如Google音乐播放器中的暂停/播放按钮。

我使用this答案来构建通知面板。请提出建议!

通知小组类:

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);
}

2 个答案:

答案 0 :(得分:2)

您应该使用计数器来确定按钮的状态。例如,让我们说:

Status1 =暂停上传方式,暂停上传图片 - 点击 - Status2 =恢复上传方式,恢复上传图片

两个状态都在一个按钮上

答案 1 :(得分:0)

我知道这是一个比较老的问题,但是看到我也在弄清楚这个问题,我想我会提供解决方案。对我来说,诀窍是找出何时更新按钮。事实证明,只有在暂停播放器之后才能执行此操作,这一点至关重要,因此我可以使用isPlaying布尔值来处理播放和切换图像。我不使用侦听器方法,因为我不想仅在控制播放时打开应用程序。这是对我有用的方法:

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
    }
}