android - 检测通知托盘中的左滑?

标签 android notifications swipe gesture

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

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

最佳答案

尝试以下

1) 创建一个接收器来处理滑动关闭事件:

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) 向您的 list 添加一个条目:

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

3) 使用唯一 id 为挂起 Intent 创建挂起 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);

关于android - 检测通知托盘中的左滑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34475256/

相关文章:

java - 滑动时加速列表滚动 - Android。如何?

angular - ionic slider - lockSwipes(true) 禁用 initialSlide 和 slideNext() 方法?

android - 从 fragment 显示对话框?

ios - UILocalNotification - 将 1 添加到当前图标角标(Badge)

javascript - 处理多个滑动事件 HTML & Javascript/jQuery

由于测试失败,Jenkins 将良好构建标记为失败

ios - 如何更新 IOS 推送通知中的角标(Badge)编号?

android - 如何以正确的方式将 UTF8 格式化为主题 header ?

java - 如何将字体更改为 Xml 中定义的默认字体?

android - 位置发生变化时如何接收系统广播?