I'm trying to set up a PendingIntent inside a PreferenceActivity in a method but Eclipse displays a red underline error "AlarmReceiverChangeImage cannot be resolved to a variable". The error is on "AlarmReceiverChangeImage" which is the name of a BroadcastReceiver.
Can you tell me how to change the code so it will not give the error on "AlarmReceiverChangeImage"?
private void setRepeatingAlarm(int time_before_changing_wallpaper) {
/*
* Set up alarm manager and get it going.
*/
AlarmManager myAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent myIntent = PendingIntent
.getBroadcast(SettingsActivity.this, 0,
AlarmReceiverChangeImage, 0);
myAlarmManager.setRepeating(
AlarmManager.RTC_WAKEUP, START_TIME,
time_before_changing_wallpaper, myIntent);
}
Update: After looking at the link provided by Kurtis I also found it will work if I made the following changes to the code.
Added this static final:
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
protected static final Intent AlarmReceiverChangeImage = null;
SharedPreferences pref;
Changed the method to look like this:
private void setRepeatingAlarm(int time_before_changing_wallpaper, Intent AlarmReceiverChangeImage) {
Changed the call to the method to look like this:
/*
* Set the default wallpaper changing time of 10
* seconds if it has not already been set.
*/
String str_list_time_before_changing_wallpaper = pref
.getString(
"list_time_before_changing_wallpaper",
"");
if (str_list_time_before_changing_wallpaper
.equals(null)) {
/*
* Set the repeating alarms.
*/
setRepeatingAlarm(EVERY_10_SECONDS, AlarmReceiverChangeImage);
}
Please look at the documetnation for the getBroadcast method. You'll notice the third argument is supposed to be an Intent, not a BroadcastReciever as you're trying to do. What you need to do is create and Intent and then put a IntentFilter on your BroadcastReciever in the manifest so that your BroadcastReciever receives the Intent when ever it's broadcast.