AppWidget alarmmanager没有更新

编程入门 行业动态 更新时间:2024-10-28 07:30:44
本文介绍了AppWidget alarmmanager没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以,我有一个小窗口,我希望它每隔60秒更新。当小部件第一次添加到主屏幕,它运行其更新的功能就好了。除此之外,它应该启动AlarmManager,这将重新更新方法每60秒。这就是它似乎并没有被实际做的部分。这是我的code:

So, I've got a widget, I want it to update every 60 seconds. When the widget is first added to the homescreen, it runs its update function just fine. Beyond that it's supposed to start an AlarmManager, which will rerun the update method every 60 seconds. That's the part that it doesn't seem to be actually doing. Here's my code:

public class ClockWidget extends AppWidgetProvider { public static String CLOCK_WIDGET_UPDATE = "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds[]) { final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clocklayout); appWidgetManager.updateAppWidget(appWidgetId, views); updateAppWidget(context, appWidgetManager, appWidgetId); } } public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { Log.d("log","Entered update cycle"); //Unimportant for these purposes appWidgetManager.updateAppWidget(appWidgetId, views); } private PendingIntent createClockTickIntent(Context context) { Intent intent = new Intent(CLOCK_WIDGET_UPDATE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } @Override public void onEnabled(Context context) { super.onEnabled(context); Log.d("onEnabled","Widget Provider enabled. Starting timer to update widget every minute"); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 60000, createClockTickIntent(context)); } @Override public void onDisabled(Context context) { super.onDisabled(context); Log.d("onDisabled", "Widget Provider disabled. Turning off timer"); AlarmManager alarmManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(createClockTickIntent(context)); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); Log.d("onReceive", "Received intent " + intent); if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) { Log.d("onReceive", "Clock update"); // Get the widget manager and ids for this widget provider, then // call the shared // clock update method. ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName()); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget); for (int appWidgetID: ids) { updateAppWidget(context, appWidgetManager, appWidgetID); } } } }

这是几个教程,我已经就此事找到的产品,和我自己的Andr​​oid的知识。据我logcats,它从来没有获取到Log.d(的onReceive,时钟更新);线。是的,我的清单已设置了时钟更新的意图。谢谢!

It's the product of a few tutorials I've found on the matter, and my own knowledge of Android. According to my logcats, it never gets to the Log.d("onReceive", "Clock update"); line. And yes, my Manifest is set up with the clock update intent. Thanks!

编辑:附加信息。我把在createClockTickIntent方法的日志行,并触发了。所以我想这意味着我的应用程序正在运行的alarmManager.setRepeating行,不知道为什么实际上没有重复。

Additional info. I put a log line in the createClockTickIntent method, and it fires off. So I guess this means that my application is running the alarmManager.setRepeating line, no idea why is isn't actually repeating.

推荐答案

Arggghhh,这是一个简单的拼写错误。这样做的目的过滤器是com.nickavv.cleanwidgets.CLEANCLOCK_UPDATE,我写了com.nickavv.cleanwidget.CLEANCLOCK_UPDATE

Arggghhh, it was a simple typo. The intent filter was "com.nickavv.cleanwidgets.CLEANCLOCK_UPDATE", and I had written "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE"

什么是痛苦,但是,嘿,现在我知道了。

What a pain, but hey, now I know.

所以,这个故事告诉任何人有类似的问题对我的:检查您的拼写!检查了两遍,还是十次。一切!

So, moral of the story for anybody with a similar problem to me: Check your spelling! Check it twice, or ten times. On everything!

更多推荐

AppWidget alarmmanager没有更新

本文发布于:2023-11-27 14:43:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AppWidget   alarmmanager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!