Android:如何在 AppWidgetProvider 中创建警报
我的小部件应该每天 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, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.HOUR_OF_DAY, 0);
cal.add(Calendar.MINUTE, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
在复制剪切的 Eclipse 的第三行中说:
The method getSystemService(String) is undefined for the type HelloWidget
HelloWidget 是 AppWidgetProvider 的名称。
谢谢
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, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.HOUR_OF_DAY, 0);
cal.add(Calendar.MINUTE, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
In the third line of the copied cut Eclipse says:
The method getSystemService(String) is undefined for the type HelloWidget
HelloWidget is the name of the AppWidgetProvider.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过吗
Did you try