Skip to content

Instantly share code, notes, and snippets.

@xanderblinov
Created November 27, 2015 12:08
Show Gist options
  • Save xanderblinov/2b7d67840dc2c856e31e to your computer and use it in GitHub Desktop.
Save xanderblinov/2b7d67840dc2c856e31e to your computer and use it in GitHub Desktop.
private static PendingIntent getAlarmIntent(Context context)
{
return PendingIntent.getService(context, 0, new AlarmIntent(context, GeoLocationService.class).putExtra(KEY_INTENT_FLAG, VALUE_START), PendingIntent.FLAG_UPDATE_CURRENT);
}
private static void cancelAlarm(Context context)
{
PendingIntent pendingIntent = getAlarmIntent(context);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
public static void start(Context context)
{
context.startService(new Intent(context, GeoLocationService.class).putExtra(KEY_INTENT_FLAG, VALUE_START));
}
public static void stop(Context context)
{
context.startService(new Intent(context, GeoLocationService.class).putExtra(KEY_INTENT_FLAG, VALUE_STOP));
}
/**
* Alarm Intent subclass needs to have single alarm instance.
* <p/>
* We can get by overriding #filterEquals method
*/
private static class AlarmIntent extends Intent
{
public AlarmIntent(Context packageContext, Class<?> cls)
{
super(packageContext, cls);
}
@Override
public boolean filterEquals(Intent other)
{
return other != null && other.getClass().equals(AlarmIntent.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment