I'm developing a test app to learn Android in which I need to fire up a service periodically to update some data. I'm using the AlarmManager
with a BroadcastReceiver
to set the alarm and it successfully updates the data with the default interval but I'd like to have this interval as a user defined value.
I currently have the following code to register the alarm:
In the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The receiver:
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = BootReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"onReceive method");
AlarmCreator.setAlarm(context, intent);
}
}
The actual alarm:
public static void setAlarm(Context context, Intent intent){
Log.d(TAG, "Setting alarm");
// I need the context here
context.startService(new Intent(context, RefreshService.class));
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
long interval = Long.parseLong(prefs.getString("interval",
Long.toString(DEFAULT_INTERVAL)));
// Here
PendingIntent operation = PendingIntent.getService(context, -1,
new Intent(context, RefreshService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
// And here
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
if (interval == 0) {
alarmManager.cancel(operation);
Log.d(TAG, "Cancelling alarm");
} else {
alarmManager.setInexactRepeating(AlarmManager.RTC,
System.currentTimeMillis(), interval, operation);
Log.d(TAG, "Setting alarm with interval: " + interval);
}
}
What I've tried
I know that using the onSharedPreferenceChanged
I can execute code when a preference is updated, the problem is that in order to set the alarm I need to have a Context
which the Preference class doesn't provide. I am aware of getActivity().getApplicationContext()
the problem is that they can return null depending on the life cycle of the Preference activity.
public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{
private SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onStart() {
super.onStart();
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals("interval")){
// Since I'm not using the intent I can just simply pass null.
AlarmCreator.setAlarm(??,null);
}
}
}
The question
What should I pass to AlarmCreator.setAlarm(Content,intent)
so that the alarm gets correctly canceled or updated? Is it even possible? Maybe there's a better approach that I'm missing.
Additional info
I just started learning Android development but I've spent a good amount of time reading the documentation and I just couldn't figure out how to do it.
There was a similar question which helped to get me on track but I found the answer too vague for my understanding at the moment.
Any Ideas would be appreciated.
Cheers.