简体   繁体   中英

How to cancel a countdown timer when a notification is clicked?

I know this has been asked a million times in other forums, but I haven't got a perfect answer yet. This is my requirement:

When the activity goes to bacground, the timer starts and after 10 sec the notification appears. If the user doesn't click on the notification after 10 sec the notification changes. When the user clicks on the first notification, the last viewed screen appears - ie the activity comes to the foreground. This is achieved by:

Intent notificationIntent = new Intent(TimedAlert.this, FirstActivity.class);

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

But i want the timer to stop immediately after the activity comes to foreground. Ie in onResume () I give timer.cancel() but this doesn't seem to work. I also tried onNewIntent() but it works only when I am on the first activity when the app goes to background from the second activity. On coming to foreground the onNewIntent() method is not called, even though I have given single top for the second activity.

How can I achieve this behavior?

当用户单击Notification ,系统将调用您的notificationIntent ,在这种情况下,系统将启动FirstActivity ,然后在FirstActivity可以停止倒计时。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM