Skip to content

Instantly share code, notes, and snippets.

@dalwadi2
Created December 31, 2019 09:49
Show Gist options
  • Save dalwadi2/a045cff2bd12812c60af28d0de5c3a73 to your computer and use it in GitHub Desktop.
Save dalwadi2/a045cff2bd12812c60af28d0de5c3a73 to your computer and use it in GitHub Desktop.
RecursiveAlarmHelper Working by Mehul Joisar
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import java.util.Date;
public class RecursiveAlarmHelper {
private static final int RECURSIVE_ALARM_ID = 9999;
@SuppressLint("NewApi")
public static void setAlarm(Context context, long trigger_time) {
Log.d("log", "setAlarm at " + new Date(trigger_time).toString());
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 23) {
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, trigger_time, getPendingIntent(context));
} else if (Build.VERSION.SDK_INT >= 21) {
AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(trigger_time, getPendingIntent(context));
am.setAlarmClock(info, getPendingIntent(context));
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, trigger_time,
getPendingIntent(context));
} else {
am.set(AlarmManager.RTC_WAKEUP, trigger_time,
getPendingIntent(context));
}
}
public static void cancelAlarm(Context context) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.cancel(getPendingIntent(context));
}
private static PendingIntent getPendingIntent(Context context) {
Intent intent = new Intent(context, RecursiveAlarmReceiver.class);
return PendingIntent.getBroadcast(context,
RECURSIVE_ALARM_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment