检测通知托盘中的向左滑动?

Roh*_*han 4 notifications android gesture swipe

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

是否有“全局”手势滑动我可以监听并且仅在我检测到我的通知被取消时触发我的特定于应用程序的事件?

44k*_*rma 8

尝试以下

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 */
  }
}
Run Code Online (Sandbox Code Playgroud)

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

<receiver
    android:name="com.my.app.receiver.NotificationDismissedReceiver"
    android:exported="false" >
</receiver>
Run Code Online (Sandbox Code Playgroud)

3) 使用挂起意图的唯一 id(此处使用通知 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;
}
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

  • 很有帮助 !!这是我正在寻找的确切解决方案:) (3认同)