Search code examples
androidalarmmanager

How to set an alarm for every 1 minute, for same say only in android?


I made one application with Android native. I'm using AlarmManager for achieving every 1-minute track data.

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

Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),100000,pendingIntent);

But I want that it must stop on specific time like today 12 P.M. or whatever time I want.

How can I achieve it by making another AlarmManager Service, or is there any other option for this?


Solution

  • Just check in service launched by Alarm that current time early than your specific time

    if (isTimeExpired) {
        AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);  
        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);    
        alarmManager.cancel(pendingIntent);
    }