Android 自定义通知按钮 onClick 不起作用

苏吉斯·曼贾瓦纳

这是我的 buildNotification 功能

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

        //this is the intent that is supposed to be called when the button is clicked
        ///////////////TOGGLE/////////////////
        Intent toggleIntent = new Intent("com.hbs.andMovie.notification.TOGGLE");
        PendingIntent pendingToggleIntent = PendingIntent.getBroadcast(this, 0, toggleIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_play, pendingToggleIntent);
        ///////////////PLAY NEXT//////////////
        Intent ffIntent = new Intent("com.hbs.andMovie.notification.FF");
        PendingIntent pendingFfIntent = PendingIntent.getBroadcast(this, 0, ffIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_ff, pendingFfIntent);
        ///////////////PLAY PREV//////////////
        Intent rewIntent = new Intent("com.hbs.andMovie.notification.REW");
        PendingIntent pendingRewIntent = PendingIntent.getBroadcast(this, 0, rewIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_rew, pendingRewIntent);
        ///////////////CLOSE//////////////////
        Intent closeIntent = new Intent("com.hbs.andMovie.notification.CLOSE");
        PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_close, pendingCloseIntent);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            String NOTIFICATION_CHANNEL_ID = "com.hbs.andmovie";
            String channelName = "DX_Player_Service";
            NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
            chan.setLightColor(Color.BLUE);
            chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            assert manager != null;
            manager.createNotificationChannel(chan);

            notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setOngoing(true)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContent(notificationView)
                    .setWhen(System.currentTimeMillis())
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .build();
        } else {
            notification = new Notification.Builder(AMPlayerCore.this)
                    .setContent(notificationView)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(Notification.PRIORITY_HIGH)
                    .build();
        }

        notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
        notification.visibility = Notification.VISIBILITY_PUBLIC;

  //the intent that is started when the notification is clicked
        Intent notificationIntent = new Intent(this, AMPlayerUI.class);
        notificationIntent.setData(uri);
        notificationIntent.putExtra(getString(R.string._mode), AMPlayerUI.mode);
        notification.contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        startForeground(1, notification);

另外我有一个 AsyncTask 每 5 秒更新一次远程视图内容..

 notificationView.setTextViewText(R.id.noti_title, title);
                    notificationView.setImageViewResource(R.id.noti_thumb, R.drawable.film_strip);
   notificationManager.notify(1, notification);

问题是按钮点击在运行 Oreo 及更高版本的设备上不起作用?它在旧设备上运行良好..

苏吉斯·曼贾瓦纳

在运行时注册广播接收器,现在它工作正常:-)

  private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            assert action != null;
            switch (action) {
                case Intent.ACTION_HEADSET_PLUG:
                    refreshDSP();
                    sendCoreBroadcast(getString(R.string._coreEventReloadDSP_UI));
                    break;
                case "android.media.AUDIO_BECOMING_NOISY":
                    if (prefs.getBoolean(context.getResources().getString(R.string._prefs_key_headphoneCanPause), true)) {
                        pause();
                        AMPlayerCore.isPaused = true;
                    }
                    break;

                case "com.hbs.andMovie.notification.TOGGLE":
                    toggle();
                    break;
                case "com.hbs.andMovie.notification.FF":
                    if (!playNext()) {
                        sendCoreBroadcast(getString(R.string._broadcastActionFailedToChangeMedia));
                    }
                    break;
                case "com.hbs.andMovie.notification.REW":
                    if (!playPrevious()) {
                        sendCoreBroadcast(getString(R.string._broadcastActionFailedToChangeMedia));
                    }
                    break;
                case "com.hbs.andMovie.notification.CLOSE":
                    close_notification();
                    break;

                default:
                    // update(context);
                    break;
            }

        }


    };

 IntentFilter ifilter = new IntentFilter();
        ifilter.addAction("com.hbs.andMovie.notification.TOGGLE");
        ifilter.addAction("com.hbs.andMovie.notification.CLOSE");
        ifilter.addAction("com.hbs.andMovie.notification.FF");
        ifilter.addAction("com.hbs.andMovie.notification.REW");
        ifilter.addAction("android.media.AUDIO_BECOMING_NOISY");
        ifilter.addAction(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(mReceiver, ifilter);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android按钮onclick不起作用

来自分类Dev

Android自定义状态按钮不起作用

来自分类Dev

Android onclick单选按钮相机意图不起作用

来自分类Dev

Onclick按钮不起作用

来自分类Dev

更改自定义适配器onClick回调中的边距不起作用,Android

来自分类Dev

onClick在android studio中不起作用

来自分类Dev

Android Volley onClick重试不起作用

来自分类Dev

Android onClick事件不起作用

来自分类Dev

onClick在ViewPager Android中不起作用

来自分类Dev

Android-RecyclerView onClick不起作用

来自分类Dev

Android FloatingActionButton“onclick”不起作用

来自分类Dev

自定义通知声音在 NotificationChannel for android P 中不起作用

来自分类Dev

按钮Onclick事件不起作用

来自分类Dev

JavaScript 按钮 onclick 不起作用

来自分类Dev

Kotlin / Anko 按钮 onClick 不起作用

来自分类Dev

按钮 onclick jquery 不起作用

来自分类Dev

自定义字体在Android中不起作用

来自分类Dev

Android:样式中的自定义颜色不起作用

来自分类Dev

Android自定义URLSpan不起作用

来自分类Dev

Android自定义方案不起作用

来自分类Dev

自定义背景资源在android中不起作用

来自分类Dev

Android导航抽屉自定义ListView不起作用

来自分类Dev

自定义视图的Android边框底部不起作用

来自分类Dev

自定义视图在Android Lollipop中不起作用

来自分类Dev

Android自定义视图onDraw不起作用

来自分类Dev

DataBinding Android,自定义设置器,不起作用?

来自分类Dev

android中的自定义listView不起作用

来自分类Dev

带有动画的android按钮中的onclick函数不起作用

来自分类Dev

如果项目具有带 onClick 的按钮,则 Android ListView setOnItemClickListener 不起作用

Related 相关文章

热门标签

归档