|
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)); |
|
} |
|
} |
|
} |