2

我有以下代码用于通知前台服务。

private Notification buildNotification(String title, String artist, Bitmap art, boolean isPlaying) {
        notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
        notificationBuilder.setOngoing(true);
        notificationBuilder.setAutoCancel(true);  //TODO revert this to false depending on effect
        notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                new Intent(getApplicationContext(), Home.class), PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(pendingIntent);

        RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
        RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);

        Intent previousTrackIntent = new Intent(getApplicationContext(), MusicService.class);
        previousTrackIntent.setAction(MusicService.ACTION_REWIND);
        PendingIntent previousTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 0, previousTrackIntent, 0);

        Intent playPauseTrackIntent = new Intent(getApplicationContext(), MusicService.class);
        playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
        PendingIntent playPauseTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 0, playPauseTrackIntent, 0);

        Intent nextTrackIntent = new Intent(getApplicationContext(), MusicService.class);
        nextTrackIntent.setAction(MusicService.ACTION_SKIP);
        PendingIntent nextTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 0, nextTrackIntent, 0);

        Intent stopServiceIntent = new Intent(getApplicationContext(), MusicService.class);
        stopServiceIntent.setAction(MusicService.ACTION_STOP);
        PendingIntent stopServicePendingIntent = PendingIntent.getService(getApplicationContext(), 0, stopServiceIntent, 0);

        if (isPlaying) {
            compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
            expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
        } else {
            compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
            expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
        }

        compactNotification.setTextViewText(R.id.notification_base_line_one, title);
        compactNotification.setTextViewText(R.id.notification_base_line_two, artist);

        expandedNotification.setTextViewText(R.id.notification_expanded_base_line_one, title);
        expandedNotification.setTextViewText(R.id.notification_expanded_base_line_two, artist);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_play, playPauseTrackPendingIntent);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_next, nextTrackPendingIntent);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_previous, previousTrackPendingIntent);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);

        expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, art);
        compactNotification.setImageViewBitmap(R.id.notification_base_image, art);

        notificationBuilder.setContent(compactNotification);

        Notification notification = notificationBuilder.build();

        if (Build.VERSION.SDK_INT >= 16)
            notification.bigContentView = expandedNotification;

        notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;

        return notification;
    }

通知会按应有的方式构建和更新,但是,当我尝试调用服务上的stopForeground(true)orstopSelf()方法时,通知仍然存在。

停止服务时如何删除此通知?

4

1 回答 1

0

您的服务的 onStartCommand 的返回值是多少?就我而言,我将值从 START_STICKY 更改为 START_NOT_STICKY,然后它就可以工作了。

于 2016-07-11T07:09:08.497 回答