简体   繁体   English

单击通知时如何取消倒数计时器?

[英]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. 当活动结束时,计时器开始计时,并在10秒钟后显示通知。 If the user doesn't click on the notification after 10 sec the notification changes. 如果10秒钟后用户未单击通知,则通知会更改。 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. 即在onResume ()我给定timer.cancel()但这似乎不起作用。 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. 我也尝试过onNewIntent()但是只有当我从第二个活动转到后台时,它才在第一个活动中起作用。 On coming to foreground the onNewIntent() method is not called, even though I have given single top for the second activity. 在前台,不会调用onNewIntent()方法,即使我为第二个活动指定了单个顶部。

How can I achieve this behavior? 我该如何实现这种行为?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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