自动重复本地通知? [英] repeating local Notifications automatically?

查看:122
本文介绍了自动重复本地通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我设法在android中创建了本地通知。下面给出了部分代码。

So I've managed to create a local notification in 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);

现在,创建通知没有问题。事实是,我目前正在通过按钮测试来进行测试,该按钮会启动整个通知,并在每天的同一时间出现通知。但是,我仍然不想添加按钮供用户按下,因此我必须删除按钮并在每次用户启动应用程序时运行此警报启动代码(在Main class的onCreate上),以确保通知已激活。但是,对我来说,一次又一次地设置相同的警报似乎不合适,因为一次就足够了。连续进行实际上会引起任何问题吗?还是有什么方法可以获取当前激活的ALARM SERVICE? (怎么样?)。我曾考虑过使用共享首选项进行一次激活,但是此期间该服务可能已停止,并且之后没有更多通知。在这种情况下,您有何建议?我看过其他一些问题几乎相同的帖子,但与我的问题无关。

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. 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 ? (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.

推荐答案

有什么办法获取当前激活的ALARM SERVICE吗?(如何?)
长话短说,不,您不能直接从Android(在调试时使用Shell进行操作,或者如果您已将设备植根,则可以) ,是的。)

"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).

但是,好消息:警报完全由其挂起意图定义,因此,相同的挂起意图和相同的警报。
您可以做的是每次以相同的EXACT挂起意图打开主活动时调用警报。此时,您有两种关于Pendin意向标志的选择:您可以使用PendingIntent.FLAG_CANCEL_CURRENT或PendingIntent.FLAG_UPDATE_CURRENT。

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. 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.

第一个将在午餐时与警报一起吃并且,如果已经注册了一个相应的待处理intet,则将完全不执行任何操作(取消将请求从当前请求中删除)

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)

第二个将与警报共进午餐,并且如果已经注册了一个相应的待处理意图,则将在每次打开应用程序时更新当前警报,并且不会添加大量警报。

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

(请参见 https://developer.android.com/reference/android/app/PendingIntent.html

这篇关于自动重复本地通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆