05b751de1906ef874b6a7232387e2b7a46585edd
[JavaForFun] /
1 package de.example.exampletdd;
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 public class WeatherInformationBootReceiver extends BroadcastReceiver {
13
14     @Override
15     public void onReceive(final Context context, final Intent intent) {
16
17         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
18                 
19                 // Update Time Rate
20             final SharedPreferences sharedPreferences = PreferenceManager
21                     .getDefaultSharedPreferences(context);
22             final String keyPreference = context
23                     .getString(R.string.weather_preferences_update_time_rate_key);
24             final String updateTimeRate = sharedPreferences.getString(keyPreference, "");            
25             long chosenInterval = 0;
26             if (updateTimeRate.equals("900")) {
27                 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
28             } else if (updateTimeRate.equals("1800")) {
29                 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
30             } else if (updateTimeRate.equals("3600")) {
31                 chosenInterval = AlarmManager.INTERVAL_HOUR;
32             } else if (updateTimeRate.equals("43200")) {
33                 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
34             } else if (updateTimeRate.equals("86400")) {
35                 chosenInterval = AlarmManager.INTERVAL_DAY;
36             }
37
38             if (chosenInterval != 0) {
39                 final AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
40                 // TODO: better use some string instead of .class? In case I change the service class
41                 // this could be a problem (I guess)
42                 final Intent serviceIntent = new Intent(context, NotificationIntentService.class);
43                 final PendingIntent alarmIntent = PendingIntent.getService(
44                                 context,
45                                 0,
46                                 serviceIntent,
47                                 PendingIntent.FLAG_UPDATE_CURRENT);
48                 alarmMgr.setInexactRepeating(
49                                 AlarmManager.ELAPSED_REALTIME,
50                                 SystemClock.elapsedRealtime() + chosenInterval,
51                                 chosenInterval,
52                                 alarmIntent);
53             }
54         }
55     }
56
57 }