56d622cdade0887bf98a652cb5caf85acb73a16e
[JavaForFun] /
1 package name.gumartinm.weather.information.boot;
2
3 import android.app.AlarmManager;
4 import android.app.PendingIntent;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.os.SystemClock;
10 import android.preference.PreferenceManager;
11
12 import name.gumartinm.weather.information.R;
13 import name.gumartinm.weather.information.notification.NotificationIntentService;
14
15 public class WeatherInformationBootReceiver extends BroadcastReceiver {
16
17     @Override
18     public void onReceive(final Context context, final Intent intent) {
19
20         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
21                 
22                 // Update Time Rate
23             final SharedPreferences sharedPreferences = PreferenceManager
24                     .getDefaultSharedPreferences(context);
25             final String keyPreference = context
26                     .getString(R.string.weather_preferences_update_time_rate_key);
27             final String updateTimeRate = sharedPreferences.getString(keyPreference, "");            
28             long chosenInterval = 0;
29             if (updateTimeRate.equals("900")) {
30                 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
31             } else if (updateTimeRate.equals("1800")) {
32                 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
33             } else if (updateTimeRate.equals("3600")) {
34                 chosenInterval = AlarmManager.INTERVAL_HOUR;
35             } else if (updateTimeRate.equals("43200")) {
36                 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
37             } else if (updateTimeRate.equals("86400")) {
38                 chosenInterval = AlarmManager.INTERVAL_DAY;
39             }
40
41             if (chosenInterval != 0) {
42                 final AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
43                 // TODO: better use some string instead of .class? In case I change the service class
44                 // this could be a problem (I guess)
45                 final Intent serviceIntent = new Intent(context, NotificationIntentService.class);
46                 final PendingIntent alarmIntent = PendingIntent.getService(
47                                 context,
48                                 0,
49                                 serviceIntent,
50                                 PendingIntent.FLAG_UPDATE_CURRENT);
51                 alarmMgr.setInexactRepeating(
52                                 AlarmManager.ELAPSED_REALTIME,
53                                 SystemClock.elapsedRealtime() + chosenInterval,
54                                 chosenInterval,
55                                 alarmIntent);
56             }
57         }
58     }
59
60 }