Closed Cornholio2108 closed 4 years ago
SnoozeReceiver.java
public class SnoozeReceiver extends BroadcastReceiver implements RoomsHandler.OnRoomsListener {
public static final String TAG = SnoozeReceiver .class.getName();
int alarmID=-1;
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: ");
this.mContext=context;
if(intent.hasExtra(ALARM_ID))
alarmID = intent.getExtras().getInt(ALARM_ID);
if(alarmID>=0) {
if(AlarmMap!=null && AlarmMap.size()>0)
{
AwakeAlarm alarm = AlarmMap.get(alarmID);
onGetOneAlarm(alarm);
}
else {
RoomsHandler.getOneAlarm(context,alarmID);
}
}
}
private void handleAlarm()
{
Intent i = new Intent(mContext, AlarmTriggerActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(ALARM_ID, alarmID);
mContext.startActivity(i);
}
@Override
public void onLoadingFinished() {
}
@Override
public void onGetOneAlarm(AwakeAlarm alarm) {
Log.d(TAG, "onGetOneAlarm: ");
if (alarm != null)
handleAlarm();
}
@Override
public void onAlarmSaved(AwakeAlarm alarm) {
}
@Override
public void onAlarmUpdated(AwakeAlarm alarm) {
}
}
Add to manifest:
<receiver
android:name=".receivers.SnoozeReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Modify AlarmTriggerActivity:
public void snoozeAlarm(){
try {
disableAlarm(true);
long alarmTime = Calendar.getInstance().getTimeInMillis()+TimeUnit.MINUTES.toMillis(myPrefs.getInt(SP_SNOOZE_DELAY,5));
Intent intent = new Intent(this, SnoozeReceiver.class);
intent.putExtra(ALARM_ID, triggeredAlarm.id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, triggeredAlarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
finish();
}
catch (Exception e)
{
Log.e(TAG, "snoozeAlarm: ", e);
}
}
Fixed
Snooze needs another AlarmReceiver without SmartShutdown, Traffic, etc