问题描述
我刚才提到很多网站,但我仍然是不能够创建通知(提示或报警)我不知道如何创建和使用它。其通知/提醒用户有关的任务,还提供每日提示给用户..我会很高兴有你的帮助,这样做,以及如何code太...
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...
问候:)Thanxs您的帮助提前。
Regards:)Thanxs for your help in advance.
推荐答案
您需要两样东西:
- AlarmManager:安排您的通知在例行基地(每日,每周,..)
- 服务:启动您的通知时,AlarmManager熄灭
下面是一个基本的例子:
Here is a basic example:
在你的活动:
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
,并把这个code在其的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);
}
这code将显示在收到报警时通知。
And this code will show the notification when the Alarm is received.
祝您好运!
这篇关于如何创建一个提醒通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!