代码之家  ›  专栏  ›  技术社区  ›  Rektirino

无法在通知[重复]中设置图像

  •  0
  • Rektirino  · 技术社区  · 6 年前

    我正在为我的音频播放器创建一个带有播放按钮的简单通知。但是我在设置位图图像时遇到问题 .setLargeIcon() 。我正试着用glide设置它。问题是,当用户跳转到下一首歌曲时,图像会发生变化,所以这几乎是一个动态视图。当我更改歌曲或暂停歌曲时,图像有时会出现(瞬间)。如果你们能帮助我,我会很高兴的!

    代码:

    生成通知:

        private void buildNotifications(PlaybackStatus playbackStatus){
    
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
    
            createChannel();
        }
    
    
    
        int notificationAction = android.R.drawable.ic_media_pause;//needs to be initialized
        PendingIntent play_pauseAction = null;
    
        //Build a new notification according to the current state of the MediaPlayer
        if (playbackStatus == PlaybackStatus.PLAYING) {
            notificationAction = android.R.drawable.ic_media_pause;
            //create the pause action
            play_pauseAction = playbackAction(1);
        } else if (playbackStatus == PlaybackStatus.PAUSED) {
            notificationAction = android.R.drawable.ic_media_play;
            //create the play action
            play_pauseAction = playbackAction(0);
        }
    
    
    
       final NotificationCompat.Builder notiB = new NotificationCompat.Builder(this,CHANNEL_ID);
        notiB.setShowWhen(false);
        notiB.setStyle(new MediaStyle()
                .setMediaSession(mediaSession.getSessionToken())
                .setShowActionsInCompactView(0,1,2));
        notiB.setSmallIcon(R.drawable.play_song);
        Glide.with(this).asBitmap().load(songInfoModelService.getAlbumIDArtwork()).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
    
                notiB.setLargeIcon(resource);
            }
        });
        notiB.setContentTitle(songInfoModelService.getSongName());
        notiB.setContentText(songInfoModelService.getArtistName());
        notiB.addAction(android.R.drawable.ic_media_previous, "previous", playbackAction(3));
        notiB.addAction(notificationAction, "pause", play_pauseAction);
        notiB.addAction(android.R.drawable.ic_media_next, "next", playbackAction(2));
    
        notification = notiB.build();
        notificationManager.notify(NOTIFICATION_ID,notification);
    
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   ADM    6 年前

    这不会同步工作。
    Glide.with(this).asBitmap().load() 是一个异步请求,因此无论当前线程如何,它都将在单独的线程上执行。所以在这种情况下 notiB.setLargeIcon(resource) 永远不会同步调用。

    解决方案可以在内部显示通知 onResourceReady() 。下载后 Bitmap 然后构建 Notification