I've set up a notification that appears in the status bar at a certain time. When you press it, it will log "yes" into a database. However I want it to log "no" if it is not pressed after 2 hours. I understand how to do the logging part, but I have no clues on how to invoke that scenario if not pressed within two hours. I know I can create two calendars to compare them, but again where would I put these? Any suggestions please? I've added some of my code below. Thanks!

Setting alarm code:

Intent intent_noti = new Intent(this,Nofi.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345, intent_noti,
        PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), setTime,
pendingIntent);

Notification code:

Intent intent = new Intent(this, ScanActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
        .setContentTitle("MedScan")
        .setContentText("2. You should take pills now")
        .setSmallIcon(R.drawable.original)
        .setContentIntent(pIntent)
        .build();
NotificationManager notificationManager = (NotificationManager) getSystemService(
        NOTIFICATION_SERVICE);
noti.defaults |= Notification.DEFAULT_ALL;
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(ID, noti);
有帮助吗?

解决方案

You can pass deleteIntent with your notification object that will be called if the user explicitly Clear your Notification

So to achieve your goal you can do the following:

  • When you display the notification, start alarm that will fire after two hours
  • Handle the two cases in which the use clicks your Notification or hit Clear and cancel the Alarm
  • If the alarm fires means your notification is still shown log no in the database
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top