Skip to content

Instantly share code, notes, and snippets.

/MainActivity Secret

Created March 20, 2016 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a9255c64b540e15f099c to your computer and use it in GitHub Desktop.
Save anonymous/a9255c64b540e15f099c to your computer and use it in GitHub Desktop.
on start button click do:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(getApplicationContext(), myService.class);
intent.setAction("start");
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0,
intent, PendingIntent.FLAG_NO_CREATE);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
on stop button click do:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(getApplicationContext(), myService.class);
intent.setAction("stop");
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0,
intent, PendingIntent.FLAG_NO_CREATE);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment