Android 如何创建提醒通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12208008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Create a Reminder Notification
提问by Rushabh
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.
问候:) 提前感谢您的帮助。
回答by iTurki
You need two things:
你需要两件事:
- AlarmManager: to schedule your notification at a regular bases (daily, weekly,..).
- Service: to launch your notification when the AlarmManager goes off.
- AlarmManager:定期(每天,每周,...)安排您的通知。
- 服务:在 AlarmManager 关闭时启动您的通知。
Here is a basic example:
这是一个基本示例:
In your Activity:
在您的活动中:
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);
This will trigger Alarm each day at midnight (12 am). You can change that if you want.
这将在每天午夜(上午 12 点)触发警报。如果你愿意,你可以改变它。
Now, create a Service NotifyService
and put this code in its onCreate()
:
现在,创建一个服务NotifyService
并将此代码放入其中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.
此代码将在收到警报时显示通知。
Good Luck!
祝你好运!
回答by Wicked161089
here is a little YouTube Video Tutorialabout daily notifications. You can find the source code in the description.
这是一个关于每日通知的YouTube 视频教程。您可以在说明中找到源代码。
This video is not made by myself. But I think its a quick help. Although i recommend some changes because the Notification.Builder is deprecated:
这个视频不是我自己做的。但我认为这是一个快速的帮助。虽然我建议进行一些更改,因为 Notification.Builder 已弃用:
1.
1.
import android.support.v4.app.NotificationCompat;
2.
2.
// Change: Notification mNotify = new Notification.Builder(this) to
Notification mNotify = new NotificationCompat.Builder(this)
Have Fun!
玩得开心!