我想检测用户何时向通知滑动 - 它可以在任何通知上,因为我将检测最近使用通知侦听器解除了哪个通知。
当我检测到我的通知被解雇时,是否存在我可以侦听的“全局”手势滑动并且仅触发我的应用特定事件?
答案 0 :(得分:2)
尝试以下
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)在清单中添加一个条目:
<receiver
android:name="com.my.app.receiver.NotificationDismissedReceiver"
android:exported="false" >
</receiver>
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;
}
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);