【问题标题】:Detect left-swipe in notification tray?检测到通知托盘中的左滑?
【发布时间】:2026-01-01 04:50:01
【问题描述】:

我想检测用户何时在通知上向左滑动 - 它可以在任何通知上,因为我将使用通知侦听器检测最近关闭的通知。

是否有一个“全局”手势滑动我可以监听,并且只有在我检测到我的通知被关闭时才会触发我的应用特定事件?

【问题讨论】:

  • AFAIK,这是不可能的,除非通过自定义 ROM。
  • 不可能,但是,您可以使用通知按钮

标签: android notifications swipe gesture


【解决方案1】:

尝试关注

1) 创建一个接收器来处理 swipe-to-dismiss 事件:

public class NotificationDismissedReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      int notificationId = intent.getExtras().getInt("com.my.app.notificationId");
      /* Your code to handle the event here */
  }
}

2) 在清单中添加一个条目:

<receiver
    android:name="com.my.app.receiver.NotificationDismissedReceiver"
    android:exported="false" >
</receiver>

3) 为待处理的 Intent 使用唯一的 id 创建待处理的 Intent(此处使用通知 ID),因为没有这个,相同的额外内容将被重复用于每个解除事件:

private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("com.my.app.notificationId", notificationId);

    PendingIntent pendingIntent =
           PendingIntent.getBroadcast(context.getApplicationContext(), 
                                      notificationId, intent, 0);
    return pendingIntent;
}

4) 构建您的通知:

Notification notification = new NotificationCompat.Builder(context)
              .setContentTitle("My App")
              .setContentText("hello world")
              .setWhen(notificationTime)
              .setDeleteIntent(createOnDismissedIntent(context, notificationId))
              .build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);

【讨论】:

  • 非常有帮助!!这是我正在寻找的确切解决方案:)