【问题标题】:How to cancel a foreground service from using the notification (swipe dismiss) or clear all notifications?如何取消前台服务使用通知(滑动关闭)或清除所有通知?
【发布时间】:2012-10-13 10:25:12
【问题描述】:

我目前正在创建一个前台服务,该服务启动时会在通知栏中显示一个通知。如果服务停止,通知会消失。我的问题是,有没有办法在“清除所有通知”或通知(滑动)消失时停止服务?

更新以包括通知的实现:

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;        
}

【问题讨论】:

  • 看看这个来自 Google 的示例:github.com/googlesamples/android-ActiveNotifications。它显示了如何侦听已关闭的通知以及整组通知。
  • 据我所知,如果不创建通知,您就无法“创建”前台服务。 Android 需要这样做,因为他们不希望您在用户不知情的情况下在后台运行服务。当您的活动被破坏并且您的服务在前台运行时删除通知需要通知,您不能简单地取消它。取消通知只能通过调用'.stopForeground(true)'取消前台服务本身来完成,布尔参数表示是否要随通知一起停止前台。

标签: android android-service android-notifications


【解决方案1】:

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

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

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

(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);

当用户将其滑开时,带有额外内容的意图将传递给服务。在onStartCommand 内处理交付的extra,即检查intent != nullintent.getExtras() != null,然后从给定someKey 的extras 包中提取值,如果匹配someIntValue,则调用stopSelf()

【讨论】:

  • 我必须使用 startForeground(false) 但我还必须从 NotificationBuilder 代码中删除对 .setOngoing(true) 的调用
【解决方案2】:

这段代码对我有用:

this.stopForeground(false);
mNotificationManager.cancel(NOTIFY_ID);

你应该设置你的onStartCommand的返回值,STRAT_STICKY会重启你的服务。

【讨论】:

  • 我们把这段代码放在哪里?,因为我们找不到通知滑动;
  • 这实际上并没有取消前台服务
【解决方案3】:

无法清除前台服务的通知。唯一的方法是停止服务。

所以我相信你想解决的问题永远不会发生。

【讨论】:

    【解决方案4】:

    我的问题是,有没有办法在“清除所有通知”或通知消失(滑动)发生时停止服务?

    假设您的Notification 设置为允许将其清除,则应在清除the deleteIntent 时调用它。您可以将其设置为getBroadcast()PendingIntent,指向一个清单注册的BroadcastReceiver,它调用stopService()

    【讨论】:

    • 谢谢,这将是我最初的方法,但是一旦服务的通知出现在通知栏中,就没有“清除所有通知”按钮,并且滑动关闭功能似乎被禁用了。跨度>
    • @moncadad:编辑您的问题,在您创建Notification 的位置发布代码,然后在此处发表评论,让我知道您发布了它。
    • @moncadad: 嗯...好吧,你没有设置FLAG_NO_CLEAR,而且我在源代码中没有看到Notification.Builder 设置FLAG_NO_CLEAR,所以Android 必须处理@987654332 @ 表示 FLAG_NO_CLEAR。顺便说一句,您不必自己设置FLAG_FOREGROUND_SERVICE——startForeground() 根据文档为您执行此操作。我一直在这些上设置FLAG_NO_CLEAR,我认为(显然是错误的)将其关闭会导致正常的Notification,如果用户清除你,你可能会失去前台权限。
    • 感谢您的帮助,因此似乎每个服务通知都不允许此行为。我想我会在通知本身中添加一个“关闭”按钮来停止服务。
    • @moncadad:上次我检查过,通知本身不支持按钮,Android 4.1 中引入的“大”样式中的操作例外。
    【解决方案5】:

    虽然我认为前台服务通知无法清除,但您可以在创建的通知中设置dismissIntent?

    【讨论】:

    • dismissintent? API 规范在哪里?
    • @IgorGanapolsky NotificationCompat.Builder#setDeleteIntent(...)
    • @Dogcat 我在 api 的任何地方都看不到 dismissIntent
    • 这是答案还是问题?
    【解决方案6】:

    我做了以下,它对我有用:

    1. 我在调用前台服务之前创建了一个通知,使用与前台服务通知相同的 id 和通知通道

    2. 我通过通知 id 取消了通知

    3. 我使用您所需的结构创建了另一个通知

    4. 您可以在不停止前台服务的情况下关闭或滑动最后一条通知

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多