如何与AlarmManager一起启动通知?

编程入门 行业动态 更新时间:2024-10-25 18:28:41
本文介绍了如何与AlarmManager一起启动通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试弄清楚如何启动通知。创建通知不是我要的,而是在后台启动通知的一种方法,这样通知就不会引起用户的干扰,用户也可以做任何事情。它的日历,提醒是确切的。注意我正在使用 AlarmManager 。

I am trying to figure out how I should launch a notification. Creating the notification is not what I am asking, but rather a way to launch it in the background so its unobtrusive and the user can do whatever they were doing. Its for a calendar, a reminder to be exact. It is also important to note I am using AlarmManager.

  • 我应该使用该方法在后台运行它。 BroadCastReciever ,服务等

    我发现的研究还提出了 AlarmManager 的问题。当应用程序被杀死或手机被关闭时,警报也会响起。为了确保该事件提醒能够确保显示通知,我还应该使用其他什么方法?

    Research I have found also presents a problem with AlarmManager. When the app is killed or phone is turned off, the alarm is also. What other method should I use in order to make sure the notification is guaranteed to show for that event reminder?

    如果需要任何其他信息,请询问,我会这样做。

    If any additional info is needed please ask and I shall do so. Thanks in advance.

    推荐答案

    创建广播接收器或intentservice。然后...

    Create a broadcastreceiver or intentservice. Then...

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Date date = new Date(); //set this to some specific time or Calendar calendar = Calendar.getInstance(); //set either of these to the correct date and time. then Intent intent = new Intent(); //set this to intent to your IntentService or BroadcastReceiver //then... PendingIntent alarmSender = PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); //or use PendingIntent.getBroadcast if you're gonna use a broadcast alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender); // date.getTime to get millis if using Date directly.

    如果您希望即使重新启动电话后这些警报也能正常工作,请添加:

    If you'd like these alarms to work correctly even when the phone is restarted, then add:

    <action android:name="android.intent.action.BOOT_COMPLETED"/>

    作为清单中Receiver上的intentfilter,并在onReceive中重新创建警报。

    as intentfilter on your Receiver in the manifest and recreate your alarms in onReceive.

    编辑

    在应用程序中创建BroadcastReceiver时,它可以做的听起来像是:在系统中接收广播。因此,例如,您可能会这样一些BroadcastReceiver:

    When you create a BroadcastReceiver in your application, it allows to do exactly what it sounds like: receive broadcasts in the system. So for example, you might some BroadcastReceiver like so:

    public class MyAwesomeBroadcastReceiver extends BroadcastReceiver { //since BroadcastReceiver is an abstract class, you must override the following: public void onReceive(Context context, Intent intent) { //this method gets called when this class receives a broadcast } }

    要向此类明确发送广播,请定义接收器清单中的内容,如下所示:

    To send broadcasts to this class explicitly, you define the receiver inside of the manifest, as follows:

    <receiver android:name="com.foo.bar.MyAwesomeBroadcastReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="SOME_AWESOME_TRIGGER_WORD"/> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>

    清单中包含以下内容可为您带来两件事:您可以在任何时候将广播明确发送给接收者想要

    Having this in the manifest gets you two things: You can send a broadcast explicitly to your receiver whenever you want by

    Intent i = new Intent("SOME_AWESOME_TRIGGER_WORD"); sendBroadcast(intent);

    此外,由于您已告知android,您希望接收BOOT_COMPLETED操作,该操作由系统,发生这种情况时,您的接收方也会被呼叫。

    Also, since you've told android you'd like to receive the BOOT_COMPLETED action which is broadcast by the system, your receiver will also get called when that happens.

  • 更多推荐

    如何与AlarmManager一起启动通知?

    本文发布于:2023-11-27 06:33:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1636977.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:通知   AlarmManager

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!