首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >自定义通知点击监听器在上述android api 26中不起作用

自定义通知点击监听器在上述android api 26中不起作用
EN

Stack Overflow用户
提问于 2018-05-23 13:11:58
回答 1查看 374关注 0票数 1

通知在api lavel 26下工作得很好,但26和更高版本的android版本的通知显示,但点击事件不工作。我有MusicPlayerReceiver来处理click。26以上接口MusicPlayerReceiver未调用。但是下面的26个完美的呼唤。我检查了stackoverflow解决方案,但对我来说都不起作用。

代码语言:javascript
复制
 private void createNotification(SuraDetail mSongDetail) {
                try {
                    String songName = mSongDetail.getTitle();
                    String authorName = mSongDetail.getArtist();
                    String albumName = mSongDetail.getDisplay_name();
                    SuraDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

                    RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_small_notification);
                    RemoteViews expandedView = null;
                    if (supportBigNotifications) {
                        expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_small_notification);
                    }

                    Intent intent = new Intent(MyApplication.applicationContext, PobitroQuranDetailsActivity.class);
                    intent.setAction("openplayer");
                    intent.setFlags(32768);
                    PendingIntent contentIntent = PendingIntent.getActivity(MyApplication.applicationContext, 0, intent, 0);

                    Notification notification = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.quran)
                            .setContentIntent(contentIntent).setContentTitle(songName).build();

                    notification.contentView = simpleContentView;
                    if (supportBigNotifications) {
                        notification.bigContentView = expandedView;
                    }

                    setListeners(simpleContentView);
                    if (supportBigNotifications) {
                        setListeners(expandedView);
                    }

                    Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(MyApplication.applicationContext) : null;

                    if (albumArt != null) {
                        notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
                        if (supportBigNotifications) {
                            notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
                        }
                    } else {
                        notification.contentView.setImageViewResource(R.id.player_album_art, R.drawable.quran);
                        if (supportBigNotifications) {
                            notification.bigContentView.setImageViewResource(R.id.player_album_art, R.drawable.quran);
                        }
                    }
                    notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
                    notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
                    notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
                    if (supportBigNotifications) {
                        notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
                        notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
                        notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
                    }

                    if (MediaController.getInstance().isAudioPaused()) {
                        notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
                        notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
                        if (supportBigNotifications) {
                            notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                            notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
                        }
                    } else {
                        notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                        notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
                        if (supportBigNotifications) {
                            notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                            notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
                        }
                    }

                    notification.contentView.setTextViewText(R.id.player_song_name, songName);
                    notification.contentView.setTextViewText(R.id.player_author_name, authorName);
                    if (supportBigNotifications) {
                        notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
                        notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
        //                notification.bigContentView.setTextViewText(R.id.player_albumname, albumName);
                    }
                    notification.flags |= Notification.FLAG_ONGOING_EVENT;
                    startForeground(1, notification);

                    if (remoteControlClient != null) {
                        RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
                        metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
                        metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
                   /*     if (audioInfo != null && audioInfo.getCover(MyApplication.applicationContext) != null) {
                            metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                                    audioInfo.getCover(MyApplication.applicationContext));
                        }*/
                        metadataEditor.apply();
                        audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            public void setListeners(RemoteViews view) {
                try {
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PREVIOUS),
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_previous, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_CLOSE), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_close, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_pause, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_NEXT), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_next, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PLAY), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_play, pendingIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
EN

回答 1

Stack Overflow用户

发布于 2018-12-17 01:45:45

我在我的应用程序中发现了完全相同的问题。这个问题显然是由意图的构造方式引起的,所以不要调用

代码语言:javascript
复制
new Intent(String action);

它现在需要调用

代码语言:javascript
复制
new Intent (String action, 
            Uri uri, 
            Context packageContext, 
            Class<?> cls)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50480259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档