报警管理器来安排的通知 [英] Schedule notification with alarm manager

查看:131
本文介绍了报警管理器来安排的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对我的alarmManager有点问题。我想注册一个警报显示在spicified时间的通知。下面是我如何做的:

I'm facing a little issue with my 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);
    }
}


我面临的问题是,notifcation出现后,我立刻运行Utils.SetAlarm无效。它也将在早上7:00出现细,所以它显然工作,但我不知道是否有人知道的方式来避免这个问题。谢谢


The issue I'm facing is that the notifcation appear immediatly after i run the 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. Thanks

推荐答案

您所面临的通知,立刻出现在运行后,上午07点的Utils.SetAlarm void.because可能有完整的设备,所以它出现在该工作instance.so尝试设置报警第二天,当当前时间大于报警时间

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

试试这个

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;
}
    am.setRepeating(AlarmManager.RTC_WAKEUP,time, AlarmManager.INTERVAL_DAY, pi);

这篇关于报警管理器来安排的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆