An alarm is triggered immediately after it is created

I try to play the ringtone at exactly 7pm every day, but it plays the ringtone right after its pending intent registers the broadcast.

I called the service in the foreground on a button click and created a pending intent there in onStartCommand:

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{

    startForeground(FOREGROUND_ID,
            buildForegroundNotification("DummyApp"));

    c = Calendar.getInstance();
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval = 1000 * 60 * 60*24;
    c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR, 19);
    c.set(Calendar.MINUTE,00);
    manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
            interval, pendingIntent);
    Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class);
    pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0,
            alarmIntent, 0);
    return START_STICKY;
}

      

Now I play the ringtone when receiving this broadcast in the DataProcessor class by the receive method of the Data Processor class:

@Override
public void onReceive(Context ctx,Intent intent) {


    playRIng(ctx);

 }

      

But when I run this code, click the button, the service is created, but the alarm is triggered immediately after calling the AlarmService and playing the ringtone. How is this possible because I am giving the exact takt time 7 O when registering the broadcast.? Googled a lot but only found the same code and nothing else. Each code can play a ringtone at a time, but also plays a ringtone immediately after registering a broadcast.

+3


source to share


4 answers


You can try the example on the developers website, knowing that there is sample code there. Maybe there is some programming error in your code, but the example code that is there works exactly as you want.



Re-alarm scheduling

0


source


Why don't you use condition ?:



@Override
public void onReceive(Context ctx,Intent intent) {
Calendar c = Calendar.getInstance(); 
 int hour = c.get(Calendar.HOUR);

if(hour==19) 
 {
    playRIng(ctx);
 }

 }

      

+2


source


If you ran this code at 9pm, you would tell AlarmManager that the intent should have been fired the first time 2 hours ago.

You need to check if the calendar time is in the current time.
If you need to add a day to the calendar so that it starts first tomorrow at 7pm.

Something like this might work:

    c = Calendar.getInstance();
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval = 1000 * 60 * 60 * 24;
    // Not needed
    // c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR, 19);
    c.set(Calendar.MINUTE, 00);

    // ** Add this **
    // Check if the Calendar 7pm is in the past
    if (c.getTimeInMillis() < System.currentTimeMillis())
        c.add(Calendar.DAY_OF_YEAR, 1); // It is so tell it to run tomorrow instead

    manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), interval, pendingIntent);
    Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class);
    pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0, alarmIntent, 0);

      

+1


source


The best way is to play with the clock and mints.

Note. Please check your time. In my case, it is formed in 24 hours.

Example:

     Calendar c = Calendar.getInstance();
   //for 12 hr formate user int hour = c.get(Calendar.HOUR);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    //Log.e("time",""+hour+":"+minute);

    if (hour == 10 && minute == 0 ) {

     }

      

0


source







All Articles