如何取消前台服务使用通知(滑动关闭)或清除所有通知? [英] How to cancel a foreground service from using the notification (swipe dismiss) or clear all notifications?

查看:76
本文介绍了如何取消前台服务使用通知(滑动关闭)或清除所有通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个前台服务,当服务启动时,该服务会出现在通知栏中.如果服务停止,通知将消失.我的问题是,有没有办法在清除所有通知"或取消通知(滑动)时停止服务?

I'm currently creating a foreground service with a notification that appears in the notification bar when the service starts. If the service stops, the notification dismisses. My question is, is there a way to stop the service when "clear all notifications" or a dismiss of the notification (swipe) occurs?

更新以包括通知的实施:

Updated to include implementation of notification:

public int onStartCommand(Intent intent, int flags, int startId)
{   
    Log.d(CLASS, "onStartCommand service started.");

    if (getString(R.string.service_start_action) == intent.getAction())
    {
        Intent intentForeground = new Intent(this, ServiceMain.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);    
        PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0);      
        Notification notification;
        Notification.Builder builder = new Notification.Builder(getApplicationContext())
            .setSmallIcon(android.R.drawable.btn_star)
            .setTicker("Service started...")
            .setContentIntent(pendIntent)
            .setDefaults(Notification.DEFAULT_ALL)
            .setOnlyAlertOnce(true)
            .setOngoing(false);
        notification = builder.build();
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

        startForeground(SERV_NOTIFY, notification);
        player.start();
    }
    else
    {
        Log.d(CLASS, "onStartCommand unable to identify acition.");
    }

    return START_STICKY;        
}

推荐答案

不允许用户刷掉正在进行的前台服务生成的通知.

The user is not allowed to swipe away a notification generated by an ongoing foreground service.

因此,首先是 stopForeground(false),这允许用户随后将通知刷掉(至少在 Lollipop+ 上).对于前棒棒糖,您可能需要 stopForeground(true),它停止前台并删除通知,然后使用 notificationManager.notify(yourNotificationID, yourNotificationObject),以便您的通知可见但可滑动.

Hence, stopForeground(false) first, which allows the notification to be swiped away by the user thereafter (at least on Lollipop+). For pre-Lollipop, you may have to stopForeground(true), which stops foreground and removes the notification, then re-issue the notification with notificationManager.notify(yourNotificationID, yourNotificationObject), so that your notification is visible but swipeable.

至关重要的是,使用删除意图设置通知对象,当用户将其滑开时会触发该意图.

Crucially, set up the notification object with a delete intent that is triggered when the user swipes it away.

(new NotificationCompat.builder(this).setDeleteIntent(deletePendingIntent)).build()

其中 deletePendingIntent 类似于

Intent deleteIntent = new Intent(this, YourService.class);
deleteIntent.putExtra(someKey, someIntValue);
PendingIntent deletePendingIntent = PendingIntent.getService(this,
someIntValue, 
deleteIntent, 
PendingIntent.FLAG_CANCEL_CURRENT);

当用户将其滑开时,Intent 及其额外内容将传递给服务.在onStartCommand中处理传递的额外内容,即检查intent != nullintent.getExtras() != null,然后从给定 someKey 的附加包,如果它匹配 someIntValue,则调用 stopSelf().

When the user swipes it away, the intent with its extra is delivered to the service. Handle the delivered extra within onStartCommand, i.e. check that intent != null, intent.getExtras() != null, then extract the value from the extras bundle given someKey, and if it matches someIntValue, then call stopSelf().

这篇关于如何取消前台服务使用通知(滑动关闭)或清除所有通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆