我想检测用户何时在通知上向左滑动——它可以在任何通知上,因为我将使用通知监听器检测最近关闭了哪个通知。
是否存在我可以监听的“全局”手势滑动,并且仅在我检测到我的通知被关闭时才触发我的应用特定事件?
最佳答案
尝试以下
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/