如何安排本地通知Android? [英] How schedule local notifications 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屋!