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

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

问题描述

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



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

解决方案

要安排本地通知,您需要知道一些用于安排通知的东西,如



BroadcastReceivers
IntentFilters
AlarmManager
NotificationService
PendingIntent

在MainActivity中,执行以下

  @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);

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

上述代码将在15秒后安排闹钟。 15秒后,它将广播notificationIntent。



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



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


i have a question about local notifications in Android. I am developing an application where in first part must receive all meetings of 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 schedule local notifications at given date?

解决方案

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

BroadcastReceivers IntentFilters AlarmManager NotificationService PendingIntent

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);
    }

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

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

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天全站免登陆