滑动以关闭前台服务

时间:2018-03-27 10:58:47

标签: java android android-notifications foregroundnotification

我试图解除前台服务的通知但却找不到任何解决方案。我没有使用 notificationManager.notify(...) ,而是使用 startForeground(...)

我的 NotificationCompat.Builder

Intent actionCancelNotification = new Intent(MusicPlayerApp.getAppContext(), NotificationReceiver.class);
actionCancelNotification.setAction(ACTION_DISMISS_NOTIFICATION);
actionCancelNotification.putExtra(PLAYBACK_NOTIFICATION_ID, PLAYBACK_NOTI_ID);

PendingIntent dismissNotiPendingIntent = PendingIntent.getBroadcast(MusicPlayerApp.getAppContext(), 0, actionCancelNotification, 0);

    //NotificationCompat.Builder
builder.setCustomBigContentView(getRemoteView())
       .setContentTitle("SongPlayback Notification")
       .setSmallIcon(R.drawable.ic_playback_notification_icon)
       .setLargeIcon(BitmapFactory.decodeResource(MusicPlayerApp.getAppContext().getResources(), R.drawable.ic_playback_notification_icon))
       .setContentText("this is content text")
       .setSubText("sub text")
       .setDeleteIntent(dismissNotiPendingIntent)
       .build();

即使我 setOnGoing(false); 它仍然无效。我已经遵循了这个线程解决方案:Make a notification from a foreground service cancelable once the service is not in foreground anymore

这是新的 NotificationCompat.Builder 作为正式文件写的:

//NotificationCompat.Builder
builder.setCustomBigContentView(getRemoteView())
       .setContentTitle("SongPlayback Notification")
       .setSmallIcon(R.drawable.ic_playback_notification_icon)
       .setLargeIcon(BitmapFactory.decodeResource(MusicPlayerApp.getAppContext().getResources(), R.drawable.ic_playback_notification_icon))
       .setContentText("this is content text")
       .setSubText("sub text")
       //this did not work too
       .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(MusicPlayerApp.getAppContext(), PlaybackStateCompat.ACTION_STOP)) //this d
       .build();

我想知道是否有任何解决方案来解决我的问题。谢谢

2 个答案:

答案 0 :(得分:0)

您可以通过轻扫以取消通知;

stopForeground(false);

并完全取消该通知;

stopForeground(true);

答案 1 :(得分:0)

此代码完全为我工作

public class TimeCounter {
    
    static int interval;
    static Timer timer;
    static int count=0;
    public static void main(String[] args) {

        
        Scanner sc = new Scanner(System.in);
        System.out.print("Input seconds => : ");
        String secs = sc.nextLine();
        long delay = 1000;
        long period = 1000;
    
        timer = new Timer();
        interval = Integer.parseInt(secs);
        
        
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println(setInterval());
            }
        }, delay, period);

    }
    
    private static final int setInterval() {
        
        
        if(interval==0) {
            timer.cancel();
            return 0;
        }
        
        if(count==2) {
            interval=interval-1;
            count=1;
        } else {
            count=count+1;
        }
        return interval;
    }
    
}