Skip to content

Commit

Permalink
Alarm: recover alarm after reboot in AlarmBootReceiver
Browse files Browse the repository at this point in the history
  • Loading branch information
deiteris committed Mar 23, 2017
1 parent 496e530 commit 9d5d317
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
10 changes: 8 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
android:allowBackup="true"
Expand Down Expand Up @@ -54,9 +55,14 @@
<service
android:name=".AlarmService"
android:enabled="true" />
<receiver android:name=".AlarmReceiver">
<receiver android:name=".AlarmReceiver" />
<receiver
android:name=".AlarmBootReceiver"
android:enabled="true"
android:exported="true"
android:label="AlarmBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/example/yink/amadeus/AlarmActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public void onToggleClicked(View view) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.d(TAG, "Current API functions have been executed");
setTime(calendar);
setTime(calendar, editor);
} else {
Log.d(TAG, "Legacy API functions have been executed");
setTimeLegacy(calendar);
setTimeLegacy(calendar, editor);
}

Log.d(TAG, "Alarm On");
Expand All @@ -72,7 +72,7 @@ public void onToggleClicked(View view) {
}

@SuppressWarnings("deprecation")
private void setTimeLegacy(Calendar calendar) {
private void setTimeLegacy(Calendar calendar, SharedPreferences.Editor editor) {
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());

Expand All @@ -81,11 +81,12 @@ private void setTimeLegacy(Calendar calendar) {
}

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
editor.putLong("alarm_time", calendar.getTimeInMillis());
Toast.makeText(this, "Alarm has been set for " + alarmTimePicker.getCurrentHour() + " hour(s) " + alarmTimePicker.getCurrentMinute() + " minute(s)", Toast.LENGTH_SHORT).show();
}

@TargetApi(Build.VERSION_CODES.M)
private void setTime(Calendar calendar) {
private void setTime(Calendar calendar, SharedPreferences.Editor editor) {
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());

Expand All @@ -94,6 +95,7 @@ private void setTime(Calendar calendar) {
}

alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
editor.putLong("alarm_time", calendar.getTimeInMillis());
Toast.makeText(this, "Alarm has been set for " + alarmTimePicker.getHour() + " hour(s) " + alarmTimePicker.getMinute() + " minute(s)", Toast.LENGTH_SHORT).show();
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/com/example/yink/amadeus/AlarmBootReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.yink.amadeus;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class AlarmBootReceiver extends WakefulBroadcastReceiver {

final String TAG = "A.AlarmBootReceiver";

@Override
public void onReceive(Context context, Intent intent) {

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())
&& settings.getBoolean("alarm_toggle", false)) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(
Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
Alarm.ALARM_ID, new Intent(context, AlarmReceiver.class), 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, settings.getLong("alarm_time", 0), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, settings.getLong("alarm_time", 0), pendingIntent);
}
Log.d(TAG, "Alarm has been recovered");
}

}
}

0 comments on commit 9d5d317

Please sign in to comment.