简体   繁体   English

如何从应用程序更新小部件?

[英]How to update widget from app?

I have an app which downloads a timetable and saves it, it also regularly downloads changes to this timetable. 我有一个可以下载时间表并保存它的应用程序,它还可以定期下载对该时间表的更改。 I'm currently using a singleton, but I find this ugly from a design perspective. 我目前正在使用单例,但是从设计角度来看,这很丑陋。 Because of the downloads I need a centralized system to minimize network usage. 由于需要下载,我需要一个集中式系统来最大程度地减少网络使用。

In the current situation, every class that requires updates from the singleton first gets an instance of this singleton and then provides an interface of which certain methods are called when an update is required. 在当前情况下,每个需要从单例更新的类都首先获取该单例的实例,然后提供一个接口,该接口在需要更新时会调用某些方法。 Currently the singleton handles all of the downloads in worker threads. 当前,单例在工作线程中处理所有下载。 Normally I would use an AsyncTask, called from the Activity, however because I have to send the result to the widget too, I can't see this work. 通常,我会使用从Activity调用的AsyncTask,但是因为我也必须将结果发送到小部件,所以看不到这项工作。

The alternative is using a Service. 替代方法是使用服务。 I would simply bind to it from both the widget and the activity. 我只是将其从小部件和活动绑定到它。 The problem in this case is that I can't bind from a BroadcastReceiver (which an AppWidgetProvider is). 在这种情况下,问题是我无法从BroadcastReceiver(AppWidgetProvider所在)进行绑定。 The issue would be that I can't regularly update my widget. 问题是我无法定期更新我的小部件。

My question is: how can I (or: can I) design my app in such a way that I can update my widget from a service (in order to get rid of the singleton) or is there a better way (maybe BroadcastReceivers)? 我的问题是:我(或:我)如何设计我的应用程序,以便可以从服务中更新小部件(以摆脱单例),或者有更好的方法(也许是BroadcastReceivers)?

how can I (or: can I) design my app in such a way that I can update my widget from a service (in order to get rid of the singleton) or is there a better way (maybe BroadcastReceivers)? 如何(或:可以)设计我的应用程序,以便可以从服务中更新小部件(以摆脱单例),或者有更好的方法(也许是BroadcastReceivers)?

To update an app widget, call updateAppWidget() on an AppWidgetManager . 要更新应用程序小部件,请在AppWidgetManager上调用updateAppWidget() This can be done at any point, not just from an AppWidgetProvider . 这可以在任何时候完成,而不仅仅是从AppWidgetProvider

To update an app widget on a periodic basis, either use android:updatePeriodMillis in your app widget metadata, or set up AlarmManager directly yourself. 要定期更新应用程序小部件,请在您的应用程序小部件元数据中使用android:updatePeriodMillis或直接AlarmManager设置AlarmManager In either case, you would want to delegate the work to an IntentService , as "downloads a timetable" is not something you want to do on the main application thread -- IntentService gives you a background thread and automatically stops itself when your work is complete, so you do not take up RAM except when you are actively doing work. 在这两种情况下,你会想工作委托给一个IntentService ,如“下载时间表”是不是你想要在主应用程序线程做一些事情- IntentService给你一个后台线程,并自动停止本身当你的工作完成,因此除非您正在积极从事工作,否则您不会占用RAM。

To do work in general on a periodic basis, use AlarmManager and an IntentService . 要定期进行一般工作,请使用AlarmManagerIntentService The IntentService can, as part of its work, update app widgets, if desired. 如果需要, IntentService可以作为其工作的一部分更新应用程序小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM