2 * Copyright 2014 Gustavo Martin Morcuende
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package name.gumartinm.weather.information.boot;
18 import android.app.AlarmManager;
19 import android.app.PendingIntent;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.os.SystemClock;
25 import android.preference.PreferenceManager;
27 import name.gumartinm.weather.information.R;
28 import name.gumartinm.weather.information.notification.NotificationIntentService;
30 public class BootReceiver extends BroadcastReceiver {
33 public void onReceive(final Context context, final Intent intent) {
35 if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
38 final SharedPreferences sharedPreferences = PreferenceManager
39 .getDefaultSharedPreferences(context);
40 final String keyPreference = context
41 .getString(R.string.weather_preferences_update_time_rate_key);
42 final String updateTimeRate = sharedPreferences.getString(keyPreference, "");
43 long chosenInterval = 0;
44 if (updateTimeRate.equals("900")) {
45 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
46 } else if (updateTimeRate.equals("1800")) {
47 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
48 } else if (updateTimeRate.equals("3600")) {
49 chosenInterval = AlarmManager.INTERVAL_HOUR;
50 } else if (updateTimeRate.equals("43200")) {
51 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
52 } else if (updateTimeRate.equals("86400")) {
53 chosenInterval = AlarmManager.INTERVAL_DAY;
56 if (chosenInterval != 0) {
57 final AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
58 // TODO: better use some string instead of .class? In case I change the service class
59 // this could be a problem (I guess)
60 final Intent serviceIntent = new Intent(context, NotificationIntentService.class);
61 final PendingIntent alarmIntent = PendingIntent.getService(
65 PendingIntent.FLAG_UPDATE_CURRENT);
66 alarmMgr.setInexactRepeating(
67 AlarmManager.ELAPSED_REALTIME,
68 SystemClock.elapsedRealtime() + chosenInterval,