Android 小部件刷新不起作用

发布于 2024-11-25 12:05:24 字数 2776 浏览 1 评论 0原文

我的小部件应该每天 0:00 刷新其文本视图。在 widget_provider.xml 中,我设置了 android:updatePeriodMillis="1000" 但我读到最小更新周期是 30 分钟,我必须为此使用 AlarmManager。所以我想要一个每天 0:00 触发刷新的闹钟。 UpdateService.class 处理刷新(根据日期为文本视图设置文本。该类直到午夜后半小时左右才会被调用)

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)< /code> 方法 我正在使用此代码:

 Intent intentN = new Intent(context, UpdateService.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentN, PendingIntent.FLAG_UPDATE_CURRENT);
 Calendar calendarN = Calendar.getInstance();
 calendarN.setTimeInMillis(System.currentTimeMillis());
 calendarN.add(Calendar.HOUR_OF_DAY, 20);
 calendarN.add(Calendar.MINUTE, 44);
 calendarN.add(Calendar.SECOND, 5);
 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarN.getTimeInMillis(), 7000, pendingIntent);

(我现在正在尝试使用实际时间)

这是此代码的正确位置吗?为什么不起作用?我在 UpdateService.java 中使用 toast 消息,但除了第一次安装应用程序时之外,我从未在 onStart(Intent Intent, int startId) 方法中获得 toast。

预先感谢

更新

在problykevin的帮助下,我重写了代码。我在 Activity 中尝试了这段代码,它可以工作:

   Intent myIntent = new Intent(MainActivity.this, UpdateService.class);
        PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);

                 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                 alarmManager.cancel(pendingIntent);
                 Calendar calendar = Calendar.getInstance();
                 calendar.setTimeInMillis(System.currentTimeMillis());
                 calendar.add(Calendar.SECOND, 10);
                 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 8000, pendingIntent);
        Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();

但是,如果我为 AppWidgetProvider 类修改它(并将其放在那里),它就不起作用。我什至从来没有收到吐司消息:

 Intent myIntent = new Intent(context, UpdateService.class);
         PendingIntent pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);

                  AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
                  alarmManager.cancel(pendingIntent);
                  Calendar calendar2 = Calendar.getInstance();
                  calendar2.setTimeInMillis(System.currentTimeMillis());
                  calendar2.add(Calendar.SECOND, 10);
                  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), 8000, pendingIntent);
         Toast.makeText(context, "Start Alarm", Toast.LENGTH_LONG).show();

My widget should refresh its textviews every day at 0:00. In the widget_provider.xml I set android:updatePeriodMillis="1000" but I read that the minimum update period is 30 minutes and I have to use alarmManager for this. So i want an alarm that triggers the refresh every day at 0:00. UpdateService.class handles the refreshing (setting texts for textviews based on date. The class is just not called until around half an hour after midnight)

In the public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) method I am using this code:

 Intent intentN = new Intent(context, UpdateService.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentN, PendingIntent.FLAG_UPDATE_CURRENT);
 Calendar calendarN = Calendar.getInstance();
 calendarN.setTimeInMillis(System.currentTimeMillis());
 calendarN.add(Calendar.HOUR_OF_DAY, 20);
 calendarN.add(Calendar.MINUTE, 44);
 calendarN.add(Calendar.SECOND, 5);
 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarN.getTimeInMillis(), 7000, pendingIntent);

(I am trying with actual time now)

Is this the right place for this code? Why doesn't it work? I am using toast messages in UpdateService.java, but i never get the toast in the onStart(Intent intent, int startId) method apart from the first time, when the app in installed.

Thanks in advance

Update

With the help of probablykevin, i rewrite the code. I tried this code out in an Activity and it works:

   Intent myIntent = new Intent(MainActivity.this, UpdateService.class);
        PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);

                 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                 alarmManager.cancel(pendingIntent);
                 Calendar calendar = Calendar.getInstance();
                 calendar.setTimeInMillis(System.currentTimeMillis());
                 calendar.add(Calendar.SECOND, 10);
                 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 8000, pendingIntent);
        Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();

However if I modify it for the AppWidgetProvider class (and put it there ofc) it does not work. I never even get the toast msg:

 Intent myIntent = new Intent(context, UpdateService.class);
         PendingIntent pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);

                  AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
                  alarmManager.cancel(pendingIntent);
                  Calendar calendar2 = Calendar.getInstance();
                  calendar2.setTimeInMillis(System.currentTimeMillis());
                  calendar2.add(Calendar.SECOND, 10);
                  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), 8000, pendingIntent);
         Toast.makeText(context, "Start Alarm", Toast.LENGTH_LONG).show();

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

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

发布评论

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

评论(1

会傲 2024-12-02 12:05:24

首先,您的重复时间应该是 24 * 60 * 60 * 1000,因为它以毫秒为单位。
其次,可以使用距下一个 0:00 的剩余时间找到开始时间:

final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000;
long timeUntilMidnight = MILLIS_IN_DAY - (System.currentTimeMillis() % MILLIS_IN_DAY);

然后通过将剩余时间添加到当前时间来设置闹钟:

alarmManager.setRepeating(...System.currentTimeMillis() + timeUntilMidnight, MILLIS_IN_DAY...);

确保您没有设置多个相同的闹钟。我喜欢

alarmManager.cancel(pendingIntent);

在再次设置之前先打电话确认一下。

First, your repeating time should be 24 * 60 * 60 * 1000, as it is in milliseconds.
Second your start time could be found using the time left until the next 0:00:

final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000;
long timeUntilMidnight = MILLIS_IN_DAY - (System.currentTimeMillis() % MILLIS_IN_DAY);

Then set your alarm by adding the time left to the current time:

alarmManager.setRepeating(...System.currentTimeMillis() + timeUntilMidnight, MILLIS_IN_DAY...);

Make sure you don't set multiple of the same alarm. I like to call

alarmManager.cancel(pendingIntent);

before setting it again just to be sure.

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