简体   繁体   中英

issue while setting time for alarm Manager

hey guys am facing a weird problem when am setting the time for my alarm manager for my notification its not giving me notification this is what am tying :

        notification = new NotificationCompat.Builder(this);
    notification.setAutoCancel(true);

    counter = (TextView) findViewById(R.id.textView);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent broadcast = PendingIntent.getBroadcast(this, 5000, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar cal = Calendar.getInstance();



    cal.add(Calendar.HOUR_OF_DAY, 22);
    cal.add(Calendar.MINUTE, 43);
    cal.add(Calendar.SECOND, 0);
    cal.add(Calendar.MILLISECOND, 0);


    // cal.add(Calendar.SECOND, 20);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

but when am setting my alarm while using only cal.add(Calendar.SECOND, 20) its giving me the notification (working fine) so any one can tell me why its not working when am using Hours, minutes and why its working only when i use Seconds only ??/ any help will be much appreciated , thanks :)

Please find the method and its params below

   setInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

if you want to repeat the alarm everyday at 22:43:00 then call like this

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

if you want to repeat the alarm every 15 mins then call like this

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);

As @Indra notes in his answer, you are incorrectly using cal.getTimeInMillis() both for the alarm trigger time and the interval. That value is not a valid interval. Presumably, you want the alarm to fire every hour, day, or some other reasonable interval. I'm guessing that because the interval you are requesting is a huge value, and the alarm you are requesting is inexact, you never see it fire even once because of this behavior explained in the documentation : "Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time". In this case "full interval" is roughly 46 years.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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