“AlarmReceiverChangeImage”方法内 PendingIntent 语句中的错误消息

发布于 2024-12-11 01:35:46 字数 2023 浏览 0 评论 0原文

我试图在方法中的 PreferenceActivity 内设置 PendingIntent,但 Eclipse 显示红色下划线错误“AlarmReceiverChangeImage 无法解析为变量”。错误发生在“AlarmReceiverChangeImage”上,它是 BroadcastReceiver 的名称。

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

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);
                        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2024-12-18 01:35:46

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文