如何创建提醒通知 [英] How to Create a Reminder Notification
问题描述
我推荐了很多网站,但仍然无法创建通知(提醒或警报)我不知道如何创建和使用它.它用于通知/提醒用户有关任务的信息,并为用户提供每日提示.我很高兴能得到你的帮助以及如何编码......
I have referred many sites but still I am not able to create the notification(reminder or alarm) I don't know exactly how to create and work with it. Its to notify/remind user about task and also provide daily tips to the user.. I will be glad to have your help in doing so and how to code it too...
问候:)提前感谢您的帮助.
Regards:) Thanxs for your help in advance.
推荐答案
你需要做两件事:
- AlarmManager:定期(每天、每周……)安排您的通知.
- 服务:在 AlarmManager 关闭时启动通知.
这是一个基本示例:
在您的活动中:
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24 , pendingIntent);
这将在每天午夜(凌晨 12 点)触发警报.如果你愿意,你可以改变它.
This will trigger Alarm each day at midnight (12 am). You can change that if you want.
现在,创建一个服务 NotifyService
并将此代码放入其 onCreate()
中:
Now, create a Service NotifyService
and put this code in its onCreate()
:
@Override
public void onCreate() {
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
Intent myIntent = new Intent(this , MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
mNM.notify(NOTIFICATION, notification);
}
此代码将在收到警报时显示通知.
And this code will show the notification when the Alarm is received.
祝你好运!
这篇关于如何创建提醒通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!