Skip to content

Instantly share code, notes, and snippets.

@cutiko
Created February 13, 2016 22:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cutiko/e2ed2109c8bf6825eabe to your computer and use it in GitHub Desktop.
Save cutiko/e2ed2109c8bf6825eabe to your computer and use it in GitHub Desktop.
How to set an alarm Android (AlarmManager)
public class AlarmBroadcaster extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Remember in the SetAlarm file we made an intent to this, this is way this work, otherwise you would have to put an action
//and here listen to the action, like in a normal receiver
createNotification(context);
}
public void createNotification(Context context) {
NotificationTrigger.starNotification(context);
}
}
//whatever goes in your manifest
//You need the wake lock so the phone can be activated
<uses-permission android:name="android.permission.WAKE_LOCK" />
//You need the permision to set the alarm
<uses-permission android:name="your.package.alarm.permission.SET_ALARM" />
//And you need the permision to listen the boot
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--This goes inside the application tags-->
<!--This is the receiver you called when the alarm was executed-->
<receiver android:name="AlarmBroadcaster" />
<!--This is the service that will run when the alarm time is reached-->
<service
android:name="NotificationTrigger"
android:exported="false" />
<!--You see how the filter here is boot completed, that is why that receiver works, cause you declared the filter here-->
<receiver
android:name="DeviceBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
new SetAlarm.alarmSetter(context);
}
}
}
}
public class NotificationTrigger extends IntentService {
private static final String ALARM = "your.package.ALARM";
public NotificationTrigger() {
super("NotificationTrigger");
}
public static void starNotification(Context context) {
Intent intent = new Intent(context, NotificationTrigger.class);
intent.setAction(ALARM);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ALARM.equals(action)) {
handleAlarm();
}
}
}
private void handleAlarm() {
PendingIntent notificationIntent = PendingIntent.getActivity(this, 0, new Intent(this, CoolClassToOpenOnAlarm.class), 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.cool_icon)
.setContentTitle("title")
.setTicker(context.getString(R.string.app_name))
.setContentText("wake up user!");
notificationBuilder.setContentIntent(notificationIntent);
notificationBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
public class SetAlarm {
public void alarmSetter(Context context) {
//This will set the alarm time to be at the 14:30
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 30);
//This intent, will execute the AlarmBroadcaster when the alarm is triggered
Intent alertIntent = new Intent(context, AlarmBroadcaster.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment