Java tutorial
package com.licenta.android.licenseapp.alarm; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.SystemClock; import android.preference.PreferenceManager; import android.support.v4.content.WakefulBroadcastReceiver; import android.util.Log; import com.licenta.android.licenseapp.SchedulingService; import com.licenta.android.licenseapp.util.Constants; /** * Created by oana.ilovan on 29.12.2015. */ public class AlarmReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, SchedulingService.class); startWakefulService(context, service); } public static void setAlarm(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); /* * If you don't have precise time requirements, use an inexact repeating alarm * the minimize the drain on the device battery. * * The call below specifies the alarm type, the trigger time, the interval at * which the alarm is fired, and the alarm's associated PendingIntent. * It uses the alarm type RTC_WAKEUP ("Real Time Clock" wake up), which wakes up * the device and triggers the alarm according to the time of the device's clock. * * Alternatively, you can use the alarm type ELAPSED_REALTIME_WAKEUP to trigger * an alarm based on how much time has elapsed since the device was booted. This * is the preferred choice if your alarm is based on elapsed time--for example, if * you simply want your alarm to fire every 60 minutes. You only need to use * RTC_WAKEUP if you want your alarm to fire at a particular date/time. Remember * that clock-based time may not translate well to other locales, and that your * app's behavior could be affected by the user changing the device's time setting. * */ // Wake up the device to fire the alarm in 30 minutes, and every 30 minutes // after that. long intervalMillis; String intervalVal = prefs.getString("repeat_interval", "0"); switch (intervalVal) { case "15": intervalMillis = AlarmManager.INTERVAL_FIFTEEN_MINUTES; break; case "30": intervalMillis = AlarmManager.INTERVAL_HALF_HOUR; break; case "60": intervalMillis = AlarmManager.INTERVAL_HOUR; break; default: intervalMillis = 0; break; } // for testing intervalMillis = 6000; alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + intervalMillis, intervalMillis, alarmIntent); prefs.edit().putBoolean(Constants.PREF_KEY_IS_ALARM_ON, true).apply(); Log.d(context.getClass().getName(), "Alarm started"); } public static void cancelAlarm(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); //if (alarmManager!= null) { alarmManager.cancel(alarmIntent); PreferenceManager.getDefaultSharedPreferences(context).edit() .putBoolean(Constants.PREF_KEY_IS_ALARM_ON, false).apply(); Log.d(context.getClass().getName(), "Alarm stopped"); //} } }