通知小图标未按预期更新

时间:2018-01-05 07:51:49

标签: android service notifications notificationmanager foregroundnotification

我正在使用foregroundService在后台播放音频,并显示带有操作的通知。

public NotificationHelper(ForegroundService service) {
    mFG_Service = service;

    mNotificationManager = (NotificationManager) mFG_Service.getSystemService(Context.NOTIFICATION_SERVICE);
}



public void showNotification(ChaptersBO chaptersBO, boolean isPlaying) {
    Notification notification = createNotification(chaptersBO, isPlaying);

    if (notification != null) {
        if (isPlaying) {
            mFG_Service.startForeground(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
        } else {
            mFG_Service.stopForeground(false);
            mNotificationManager.notify(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
        }
    }
}


private Notification createNotification(ChaptersBO chapter, boolean isPlaying) {
    if (chapter == null)
        return null;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mFG_Service);
    try {
        Bitmap sourceBitmap = BitmapFactory.decodeResource(mFG_Service.getResources(), R.drawable.notification_logo);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(sourceBitmap, 300, 300, false);
        if (resizedBitmap != sourceBitmap) {
            sourceBitmap.recycle();
        }

        builder.setContentTitle("Audio")
                .setContentText(chapter.getChapterNumber())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setSmallIcon(getSmallIcon(isPlaying))
                .setLargeIcon(resizedBitmap)
                .setShowWhen(false)
                .setOngoing(isPlaying)
                .setContentIntent(getPendingIntent())
                .addAction(R.drawable.ic_notif_prev, "", getAction(ACTION.PREV_ACTION, REQ_CODE_PREVIOUS))
                .addAction(getSmallIcon(isPlaying), "", getAction(ACTION.PLAY_PAUSE_ACTION, REQ_CODE_PLAY_OR_PAUSE))
                .addAction(R.drawable.ic_notif_next, "", getAction(ACTION.NEXT_ACTION, REQ_CODE_NEXT))
                .setDeleteIntent(getStopIntent())
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

    } catch (Exception e) {
        e.printStackTrace();
    }
    return builder.build();

}

我想在播放/暂停时更新通知小图标中心操作图标。它第一次工作正常,但如果我清除通知并播放另一个音频,那么这些图标(在下面的屏幕截图中圈出)不会更新。

enter image description here

暂停音频我称之为:

mNotificationManager.notify(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);

如果我用这个

替换它
mFG_Service.startForeground(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);

然后它工作正常但在这种情况下我无法清除通知。

任何有关在播放/暂停时更新这些图标以及在暂停音频时移除通知的建议都将受到赞赏。

0 个答案:

没有答案
相关问题