[英]Schedule notification with alarm manager
I'm facing a little issue with my alarmManager. 我的alarmManager面临一个小问题。 I'm trying to register an alarm to display a notification at spicified time.
我正在尝试注册警报以在特定时间显示通知。 Here is how I'm doing it:
这是我的做法:
Utils.SetAlarm(this, Utils.GOODMORNING, 7, 0, 0); // so at 7:00
public static void SetAlarm(Context context, int req, int hour, int minute, int second) {
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
Intent intent = new Intent(context, myService.class);
intent.putExtra(ACTION_REQUEST, req);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}
public class myService extends IntentService {
private static final int GOODMORNING_NOTIFICATION_REQUEST = 517;
public myService() {
super("myService");
}
@Override
protected void onHandleIntent(Intent intent) {
int extra = intent.getIntExtra(Utils.ACTION_REQUEST, 0);
if (extra == Utils.GOODMORNING) {
Notification notification = new Notification.Builder(this)
.setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(GOODMORNING_NOTIFICATION_REQUEST, notification);
}
}
The issue I'm facing is that the notifcation appear immediatly after i run the Utils.SetAlarm void. 我面临的问题是,在运行Utils.SetAlarm void之后,通知立即出现。 It will also appear fine at 7:00 am so it's apparently working but i wonder if someone known a way to avoid this problem.
它也将在上午7:00正常显示,因此显然可以正常工作,但我想知道是否有人知道一种避免此问题的方法。 Thanks
谢谢
your were facing notification appear immediatly after you run the Utils.SetAlarm void.because of 7:00 am may have complete in your device so it's appears working at that instance.so try to set alarm for next day when current time is greater than alarm time 运行Utils.SetAlarm无效后,您面临的通知立即显示。由于上午7:00可能已在您的设备中完成操作,因此该设备似乎在该实例上正常工作。因此,请尝试在当前时间大于警报的第二天设置警报时间
try this 尝试这个
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
long time=calendar.getTimeInMillis();
if(time<System.currentTimeMillis()){
time=time+AlarmManager.INTERVAL_DAY;
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,time,
AlarmManager.INTERVAL_DAY, alarmIntent);
Use following code for your solution : and i used comments so you can easily understand 为您的解决方案使用以下代码:我使用了注释,因此您可以轻松理解
// time at which alarm will be scheduled here alarm is scheduled at 1
// day from current time,
// we fetch the current time in milliseconds and added 1 day time
// i.e. 24*60*60*1000= 86,400,000 milliseconds in a day
Long time = selectedMilli;
// create an Intent and set the class which will execute when Alarm
// triggers, here we have
// given AlarmReciever in the Intent, the onRecieve() method of this
// class will execute when
// alarm triggers and
// we will write the code to send SMS inside onRecieve() method pf
// Alarmreciever class
Intent intentAlarm = new Intent(ScheduleActivity1.this,
AlarmReciever.class);
// create the object
AlarmManager alarmManager = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
// set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent
.getBroadcast(this, i, intentAlarm,
PendingIntent.FLAG_UPDATE_CURRENT));
/*
* alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
* time,TimeUnit.MINUTES.toMillis(1), PendingIntent.getBroadcast(this,
* i, intentAlarm,PendingIntent.FLAG_UPDATE_CURRENT));
*/
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
// time,AlarmManager.INTERVAL_DAY, PendingIntent.getBroadcast(this, i,
// intentAlarm,PendingIntent.FLAG_UPDATE_CURRENT));
// Toast.makeText(ScheduleActivity1.this,
// "Alarm Scheduled for " + selectedMilli, Toast.LENGTH_LONG)
// .show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.