Akash Pandya
Akash Pandya

Reputation: 13

Will my code to set an alarm at a specific time daily work for the next day as well?

I want to set alarm for 10:45 AM daily, but the interval provided in setRepeat() is not working if I put 5*1000 (5 seconds) as well. Here is my code:

public void SetAlarm()
{
   
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override public void onReceive( Context context, Intent _ )
        {
            Toast.makeText(context, "hi", Toast.LENGTH_SHORT).show();
            tex.setText(s[i+1]);
            i++;

            Notification.Builder n  = new Notification.Builder(context)
                    .setContentTitle("Today's Quote")
                    .setContentText(tex.getText())
                    .setSmallIcon(R.drawable.ic_lightbulb_outline_black_24dp)
                    .setContentIntent(PendingIntent.getActivity(context,0,new Intent(""),0))
                    .setSound(uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setAutoCancel(true);


            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(1, n.build());

            context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity
        }
    };

    this.registerReceiver( receiver, new IntentFilter("com.blah.blah.somemessage") );

    PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("com.blah.blah.somemessage"), 0 );
    AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 10);
    calendar.set(Calendar.MINUTE, 45);
 manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,Calendar.getInstance().getTimeInMillis()+AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_DAY, pintent);
}
  1. This will fire an alarm at 11:00 first
  2. Will it fire an alarm everyday.

Upvotes: 0

Views: 199

Answers (1)

tejendra singh
tejendra singh

Reputation: 84

Use

manager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000, pi); 

instead of

manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,Calendar.getInstance().getTimeInMillis()+AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_DAY, pintent);

Upvotes: 0

Related Questions