简体   繁体   English

自动重复本地通知?

[英]repeating local Notifications automatically?

So I've managed to create a local notification in android. 所以我设法在android中创建本地通知。 Part of the code is given below. 下面给出了部分代码。

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),alarmManager.INTERVAL_DAY,pendingIntent);

Now no problem with creating the notification. 现在,创建通知没有问题。 The thing is that I am currently testing it with a button press which starts the whole notification thing with the Notification appearing every day at the the same time. 事实是,我目前正在通过按钮测试来进行测试,该按钮会启动整个通知,并在每天的同一时间出现通知。 But, still I don't want to add a button for users to press so I'd have to remove the button and just run this alarm initiation code every time the user starts the app (on Main class' onCreate) just to make sure that notifications are activated. 但是,我仍然不想添加按钮供用户按下,因此我必须删除按钮并在每次用户启动应用程序时运行此警报启动代码(在Main class的onCreate上),以确保通知已激活。 Yet, it does not seem proper to me to set the same alarm over and over since a single time would be enough. 但是,对我来说,一次又一次地设置相同的警报似乎不合适,因为一次就足够了。 Does it actually cause any problems to be doing that continuously ? 连续进行实际上会引起任何问题吗? Or is there any way to get the current activated ALARM SERVICE ? 还是有什么方法可以获取当前激活的ALARM SERVICE? (how?). (怎么样?)。 I was thinking about using shared preferences for a single time activation, but the Service might be stopped in the meantime and no more notifications then. 我曾考虑过使用共享首选项进行一次激活,但是此期间该服务可能已停止,并且之后没有更多通知。 What do you suggest in such a scenario ? 在这种情况下,您有何建议? I have seen some other posts with nearly same question but it doesn't relate to mine. 我看过其他一些问题几乎相同的帖子,但这与我的无关。

"is there any way to get the current activated ALARM SERVICE ? (how?)" Long story short, No you can't directly from Android (you can do it with the shell, while debugging OR if you have rooted your device, in this case, yes). “有什么方法可以获取当前激活的ALARM SERVICE吗?(如何?)”长话短说,不,您不能直接从Android(可以在调试时使用Shell进行操作,或者如果您已将设备植根于其中,这种情况,是的)。

BUT, good news: an alarm is fully defined by its pending intent, hence, same pending intent, same alarm. 但是,好消息:警报完全由其未决意图定义,因此,相同的未决意图和相同的警报。 What you can do is to call your alarm each time you open the main activity with the same EXACT pending intent. 您可以做的是,每次以相同的EXACT待定意图打开主要活动时都将发出警报。 at this point you have 2 choice about the flag of the pendin intent: you can use PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_UPDATE_CURRENT. 在这一点上,关于pendin意向标志有两种选择:可以使用PendingIntent.FLAG_CANCEL_CURRENT或PendingIntent.FLAG_UPDATE_CURRENT。

The first one will get lunched with the alarm and, if there's one corresponding pending intet allready registered, will simply do nothing (cancell the request leaving the current one) 第一个将与警报共进午餐,并且如果已经注册了一个相应的待处理intet,则将不执行任何操作(取消请求以保留当前的请求)

The second one will get lunched with the alarm and, if there's one corresponding pending intent already registered, will update the present alarm and won't add a ton of alarms each time you open your app. 第二个将与警报共进午餐,如果已经注册了一个相应的待处理意图,它将更新当前警报,并且每次打开应用程序时都不会添加大量警报。

Personally, the second one has always done his job for me 就个人而言,第二个人一直为我完成他的工作

(see https://developer.android.com/reference/android/app/PendingIntent.html ) (请参阅https://developer.android.com/reference/android/app/PendingIntent.html

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

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