简体   繁体   English

使用AlarmManager发出火灾通知

[英]Fire notification with AlarmManager

I am using AlarmManager to fire Notification. 我正在使用AlarmManager触发通知。 I want that notification fires at 10:30 am of the morning and repeat at every 24 hours. 我希望该通知在上午10:30触发,并每24小时重复一次。 I don't want notification when opening application. 打开应用程序时我不希望收到通知。

I am posting my code, main problem is that It fires alarm in the nite time also at 10:30 (if phone timezone is of 12 hours). 我正在发布代码,主要问题是它也会在晚上10点30分(如果电话时区为12小时)在夜间触发警报。 I check this code with modification yest and I got alarm yest nite at 10:30, 1, 4, 7. 我通过修改yest来检查此代码,并在10:30、1、4、7收到了yestn警报。

Please help me solve the problem, I am trying to solve it from the long time. 请帮助我解决问题,我正试图从很长一段时间解决问题。

Code : 代码:

Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this,
            0, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Calendar firingCal = Calendar.getInstance();
    Calendar currentCal = Calendar.getInstance();

    firingCal.set(Calendar.HOUR_OF_DAY, 10);
    firingCal.set(Calendar.MINUTE, 30);
    firingCal.set(Calendar.SECOND, 0);

    long intendedTime = firingCal.getTimeInMillis();
    long currentTime = currentCal.getTimeInMillis();

    if (intendedTime >= currentTime) {
        alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
                AlarmManager.INTERVAL_DAY, pendingIntent);

    } else {
        firingCal.add(Calendar.DAY_OF_MONTH, 1);
        intendedTime = firingCal.getTimeInMillis();

        alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
                AlarmManager.INTERVAL_DAY, pendingIntent);
    }

Try using: 尝试使用:

firingCal.set(Calendar.HOUR_OF_DAY, 10);  

because this is the hour in 24hour notation. 因为这是24小时制中的小时数

It also seems you forget to add a day in the else statement because the if and the else do exactly the same now. 似乎您也忘记了在else语句中添加一天,因为if和else现在完全一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM