Skip to content

Instantly share code, notes, and snippets.

@MrCrambo
Created February 10, 2020 14:35
Show Gist options
  • Save MrCrambo/835543a716f3f4cc2b250c86f6b4478d to your computer and use it in GitHub Desktop.
Save MrCrambo/835543a716f3f4cc2b250c86f6b4478d to your computer and use it in GitHub Desktop.
public class NavigineBroadcastReceiverExample extends BroadcastReceiver {
// here you need to receive some action from intent and depending on this action start service or set repeating alarm
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
try{
if (action.equals("START")) {
Intent wakeIntent = new Intent("WAKE");
wakeIntent.setClass(context, YourReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, 0,
wakeIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmService = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if (alarmService != null)
alarmService.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 15 * 60 * 1000, pendingIntent);
} else if (action.equals("WAKE")) {
JobScheduler jobShedulerService = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobShedulerService.schedule(new JobInfo.Builder(1, new ComponentName(context, YourJobClass.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setMinimumLatency(0L)
.setOverrideDeadline(15 * 60 * 1000)
.build());
Intent wakeIntent = new Intent("WAKE");
wakeIntent.setClass(context, YourReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, 0,
wakeIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmService = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if (alarmService != null)
alarmService.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Default.NAVIGINE_JOB_SERVICE_WAKEUP_TIME * 1000, pendingIntent);
}
} catch (Throwable e) {
Logger.d(TAG, 1, Log.getStackTraceString(e));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment