forked from Yink/Amadeus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alarm: recover alarm after reboot in AlarmBootReceiver
- Loading branch information
Showing
3 changed files
with
52 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/yink/amadeus/AlarmBootReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} | ||
} |