简体   繁体   English

为Android创建提醒通知

[英]Create Reminder Notification for Android

I know how to create scheduled notification in Android by using alarm service but what I want to do now is to create notification in more frequent way. 我知道如何通过使用警报服务在Android中创建计划的通知,但是我现在想做的是以更频繁的方式创建通知。

For example, I want to push notification for 8 hours at the interval of 20 mins. 例如,我想以20分钟的间隔将通知推送8小时。 In this case, is it efficient to use alarm service or timertask will be the better choice? 在这种情况下,使用警报服务效率高还是定时器任务将是更好的选择?

No matter which method, I wish to able to cancel it in the halfway. 无论采用哪种方法,我都希望能够在中途取消。 Thanks. 谢谢。

Timertask starts new thread and works in it. Timertask启动新线程并在其中运行。 So if your app will work in background and your app will be closed by android, you won't receive any notification. 因此,如果您的应用程序将在后台运行并且您的应用程序将被android关闭,则您将不会收到任何通知。 AlarmManager provides access to the system alarm services. AlarmManager提供对系统警报服务的访问。 When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. 警报响起时,系统会广播已为其注册的Intent,并在目标应用程序尚未运行时自动启动它。 ( link ). 链接 )。 So it will start your app even if it was closed. 因此,即使它已关闭,它也会启动您的应用程序。 And you have to understand how you app will work with this notifications. 而且,您必须了解您的应用将如何处理此通知。 If it works only while user works in app, you can use timertask, but if it has to work in background(for example you will receive notification even if user doesn't work with a phone/tablet), it will be better use alarmanager. 如果仅当用户在应用程序中工作时它可以工作,则可以使用timertask,但是如果它必须在后台工作(例如,即使用户不使用手机/平板电脑,您也会收到通知),最好使用Alarmanager 。 Hope it helps. 希望能帮助到你。

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

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