1

我正在尝试在方法中的 PreferenceActivity 中设置 PendingIntent,但 Eclipse 显示红色下划线错误“AlarmReceiverChangeImage 无法解析为变量”。错误出现在“AlarmReceiverChangeImage”上,它是广播接收器的名称。

您能告诉我如何更改代码,使其不会在“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);
}

更新:查看 Kurtis 提供的链接后,我还发现如果我对代码进行以下更改,它将起作用。

添加了这个静态决赛:

公共类 SettingsActivity 扩展 PreferenceActivity 实现 OnSharedPreferenceChangeListener {

protected static final Intent AlarmReceiverChangeImage = null;

SharedPreferences pref;

将方法更改为如下所示:

private void setRepeatingAlarm(int time_before_changing_wallpaper, Intent AlarmReceiverChangeImage) {

将对该方法的调用更改为如下所示:

                        /*
                         * 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);
                        }
4

1 回答 1

1

请查看getBroadcast方法的文档。您会注意到第三个参数应该是一个 Intent,而不是您尝试做的 BroadcastReciever。您需要做的是创建和 Intent,然后在清单中的 BroadcastReciever 上放置一个 IntentFilter,以便您的 BroadcastReciever 在广播时接收 Intent。

于 2011-10-19T21:43:01.557 回答