Android如何安排本地通知? [英] How schedule local notifications Android?

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

问题描述

我有一个关于 Android 本地通知的问题.我正在开发一个应用程序,其中第一部分我必须接收我自己服务器的公司的所有会议(我已经实现了),第二部分我必须在每次会议前一天通知,但有本地通知.

I have a question about local notifications in Android. I am developing an application where in the first part I must receive all meetings of the company of my own server (this I have achieved), and the second part I must notify one day before each meeting, but with local notifications.

如何在给定日期安排本地通知?

How to schedule local notifications at a given date?

推荐答案

要安排本地通知,您需要了解一些用于安排通知的事情,例如

To schedule local notification you need to know some of the things which are used to schedule the notification like

广播接收器意图过滤器报警管理器通知服务PendingIntent

BroadcastReceivers IntentFilters AlarmManager NotificationService PendingIntent

在 MainActivity 中执行以下操作

In MainActivity do the following

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.addCategory("android.intent.category.DEFAULT");

        PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 15);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
    }

以上代码将在 15 秒后安排闹钟.15 秒后,它将广播通知意图.

The above code will schedule an alarm after 15 seconds. After 15 seconds it will broadcast the notificationIntent.

Intent 构造函数中指定的动作在 AndroidManifest.xml 中定义

The action specified in the Intent constructor is defined in the AndroidManifest.xml

要了解本地通知的完整工作并查看示例通知代码,请查看这篇文章 - http://www.singhajit.com/schedule-local-notification-in-android/

To understand the full working of local notification and to see a sample notification code check out this article - http://www.singhajit.com/schedule-local-notification-in-android/

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

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