如何更改通知按钮中的清除通知标志单击

时间:2018-08-29 11:34:52

标签: android notifications broadcastreceiver exoplayer remoteview

我正在开发流应用程序。为此,我正在使用Exoplayer,因此在播放显示通知时。在此通知中,有播放/暂停按钮。每当我们暂停音乐通知时,都需要启用清除模式,而每次播放音乐通知时,都不应清除。

这是通知代码

static RemoteViews notificationView;
static Notification notification;
static NotificationManager notificationManager;

private void startNotification() {
        String ns = Context.NOTIFICATION_SERVICE;
        notificationManager =
                (NotificationManager) getSystemService(ns);

        notification = new Notification(R.mipmap.ic_launcher, null,
                System.currentTimeMillis());

        notificationView = new RemoteViews(getPackageName(),
                R.layout.mynotification);

        if (isPlaying)
            notificationView.setInt(R.id.closeOnFlash, "setBackgroundResource", R.drawable.pausecontrol);
        else
            notificationView.setInt(R.id.closeOnFlash, "setBackgroundResource", R.drawable.playcontrol);

        notificationView.setTextViewText(R.id.appName, "Testing......");

        //the intent that is started when the notification is clicked (works)
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.putExtra("play", "notify");

        editor = preferences.edit();
        editor.putString("play", "notify");
        editor.apply();

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 11,
                notificationIntent, 0);

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

        if (isPlaying)
            notification.flags |= Notification.FLAG_NO_CLEAR;
        else
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

        //this is the intent that is supposed to be called when the
        //button is clicked
        Intent intentPlay = new Intent(this, switchButtonListener.class);
        PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0
                intentPlay, PendingIntent.FLAG_NO_CREATE);
        notificationView.setOnClickPendingIntent(R.id.closeOnFlash, pendingSwitchIntent);


        notificationManager.notify(1, notification);
    }

这是接收方代码

public static class switchButtonListener extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
                Log.d("Here", "I am play");
                if (exoPlayer.getPlayWhenReady()) {
                 exoPlayer.setPlayWhenReady(false);
                 btnPlay.setImageResource(android.R.drawable.ic_media_play);
                 notificationView.setInt(R.id.closeOnFlash, "setBackgroundResource", R.drawable.playcontrol);
                 notification.flags |= Notification.FLAG_AUTO_CANCEL;                    
                 notificationManager.notify(1, notification);
                 }else 
                 {
                exoPlayer.setPlayWhenReady(true);
                btnPlay.setImageResource(android.R.drawable.ic_media_pause);
                notificationView.setInt(R.id.closeOnFlash, "setBackgroundResource", R.drawable.pausecontrol);
               notification.flags |= Notification.FLAG_NO_CLEAR;
               notificationManager.notify(1, notification);
                }

        }
    }

这里的问题是,无论何时单击通知上的播放/暂停按钮,然后需要启用/禁用通知清除模式。但这是行不通的,但是无论何时我们在活动中单击“播放/暂停”按钮都可以正常工作,为什么因为当我单击显示通知的按钮时,在该通知方法中我会像这样检查

   if (isPlaying)
        notification.flags |= Notification.FLAG_NO_CLEAR;
    else
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

因此,在活动按钮中单击通知启用/禁用正常,但是在通知按钮中单击不起作用。但播放/暂停按钮图像也成功更改,唯一的问题是通知启用/禁用不起作用。但是这里的另一个问题是,当我们暂停通知按钮中的音乐,然后单击打开的通知应用程序并且通知也关闭时。因此,这里的问题是无法更新接收器中的清除标志。

所以,请先谢谢我指导。

0 个答案:

没有答案