我已经实现了一个小部件,但在刷新它时遇到了问题。我得到了最初的 onUpdate ,如果我离开它很长时间,我也会再次得到它。
但是,我希望我的主应用程序中的更改自动反映在小部件上。我如何让它刷新?有没有办法从我的活动中调用小部件(我只是做一个静态例程)还是有一种方法可以强制每次手机返回主屏幕时调用我的“onUpdate”?
您是否使用服务来更新 RemoteViews 实现了您的小部件?如果是这样,您可以使用 Intent 启动该服务,并且小部件将更新。
Intent widgetUpdateIntent = new Intent(context, MyClassName.UpdateService.class);
context.startService(widgetUpdateIntent );
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager//.setRepeating(AlarmManager.RTC_WAKEUP, 500, 2, pendingIntent1);
.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 500, pendingIntent1);