- Deal with time and timezone changes
This commit is contained in:
		| @@ -46,10 +46,12 @@ | ||||
|         <receiver android:name="za.org.treehouse.hypoalarm.GraceReceiver" /> | ||||
|         <receiver android:name="za.org.treehouse.hypoalarm.CancelGraceReceiver" /> | ||||
|         <receiver | ||||
|             android:name="za.org.treehouse.hypoalarm.BootReceiver" | ||||
|             android:name="za.org.treehouse.hypoalarm.AlarmChangeReceiver" | ||||
|             android:enabled="false" > | ||||
|             <intent-filter> | ||||
|                 <action android:name="android.intent.action.BOOT_COMPLETED" /> | ||||
|                 <action android:name="android.intent.action.TIMEZONE_CHANGED" /> | ||||
|                 <action android:name="android.intent.action.TIME_SET" /> | ||||
|             </intent-filter> | ||||
|         </receiver> | ||||
|  | ||||
|   | ||||
| @@ -9,14 +9,16 @@ import android.util.Log; | ||||
| 
 | ||||
| import java.util.Calendar; | ||||
| 
 | ||||
| public class BootReceiver extends BroadcastReceiver { | ||||
| public class AlarmChangeReceiver extends BroadcastReceiver { | ||||
|     @Override | ||||
|     public void onReceive(Context context, Intent intent) { | ||||
|         SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | ||||
|         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { | ||||
|         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED") || | ||||
|                 intent.getAction().equals("android.intent.action.TIMEZONE_CHANGED") || | ||||
|                 intent.getAction().equals("android.intent.action.TIME_SET")) { | ||||
|             String alarmTimeStr = sharedPref.getString(context.getString(R.string.AlarmTimePref), null); | ||||
|             Calendar cal = MainActivity.TimeStringToCalendar(alarmTimeStr); | ||||
|             Log.d("BootReceiver", "Booting: Setting alarm for " + MainActivity.debugDate(cal)); | ||||
|             Log.d("AlarmChangeReceiver", intent.getAction() + ": resetting alarm for " + MainActivity.debugDate(cal)); | ||||
|             MainActivity.resetAlarm(context, cal); | ||||
|         } | ||||
|     } | ||||
| @@ -342,13 +342,13 @@ public class MainActivity extends ActionBarActivity { | ||||
|             } | ||||
|             Log.d("MainActivity", "Setting pre-alarm for " + MainActivity.debugDate(preNotifyCal)); | ||||
|  | ||||
|             // Set boot receiver, so alarm restarts on boot | ||||
|             ComponentName bootReceiver = new ComponentName(context, BootReceiver.class); | ||||
|             // Set time change and boot receiver, so alarm restarts on boot | ||||
|             ComponentName bootReceiver = new ComponentName(context, AlarmChangeReceiver.class); | ||||
|             PackageManager pm = context.getPackageManager(); | ||||
|             pm.setComponentEnabledSetting(bootReceiver, | ||||
|                     PackageManager.COMPONENT_ENABLED_STATE_ENABLED, | ||||
|                     PackageManager.DONT_KILL_APP); | ||||
|             Log.d("MainActivity", "Setting BootReceiver"); | ||||
|             Log.d("MainActivity", "Setting boot receiver"); | ||||
|  | ||||
|         } | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user