【Android】桌面Widget动态刷新

关于桌面widget的动态刷新有这样一个配置,

在res/xml下<appwiget-provider>下有一个属性

android:updatePeriodMillis="86400000"

官方的注释是:

The updatePeriodMillis attribute defines how often the App Widget framework should request an update from the AppWidgetProvider by calling the onUpdate() callback method. The actual update is not guaranteed to occur exactly on time with this value and we suggest updating as infrequently as possible—perhaps no more than once an hour to conserve the battery. You might also allow the user to adjust the frequency in a configuration—some people might want a stock ticker to update every 15 minutes, or maybe only four times a day.

中文翻译:

updatePeriodMillis这个属性规定了AppWiget框架应该多久发送一次请求通过调用AppwidgetProvider的onUpdate()方法来更新,实际的更新并不能保证根据这个值准时的更新,我们建议尽可能频率少的进行更新操作,最好不要超过1个小时更新一次,为了保存电池的电量。你页可以给出配置允许用户来调整更新的频率,就像一些人希望股票行情追踪器每15分钟更新一次,或者仅仅一天更新4次

另外还给出了一个备注:

Note: If the device is asleep when it is time for an update (as defined by updatePeriodMillis), then the device will wake up in order to perform the update. If you don't update more than once per hour, this probably won't cause significant problems for the battery life. If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates b
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
创建 Android 应用程序中的 App Widget,需要以下步骤: 1. 创建 App Widget Provider 类 ```java public class MyWidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // 更新 App Widget RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); views.setTextViewText(R.id.tv_widget, "Hello World!"); appWidgetManager.updateAppWidget(appWidgetIds, views); } } ``` 2. 声明 App Widget Provider 在 AndroidManifest.xml 文件中声明 App Widget Provider,如下所示: ```xml <receiver android:name=".MyWidgetProvider" android:label="@string/app_name"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_widget_info" /> </receiver> ``` 其中,MyWidgetProvider 是你的 App Widget Provider 类名,@string/app_name 是你应用的名称,@xml/my_widget_info 是你的 App Widget Provider 的相关信息,需要在 res/xml 目录下创建一个名为 my_widget_info.xml 的文件,并在该文件中指定你的 App Widget 的布局和其他属性。 3. 定义 App Widget 的布局 在 res/layout 目录下创建一个名为 widget_layout.xml 的文件,定义 App Widget 的布局,如下所示: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <TextView android:id="@+id/tv_widget" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="#000000" /> </RelativeLayout> ``` 4. 更新 App Widget 在 onUpdate() 方法中,更新 App Widget 的 UI。可以使用 RemoteViews 对象更新 App Widget 的 UI,如下所示: ```java @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // 更新 App Widget RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); views.setTextViewText(R.id.tv_widget, "Hello World!"); appWidgetManager.updateAppWidget(appWidgetIds, views); } ``` 5. 安装 App Widget 在应用的首次安装时,需要向用户请求授权,以便创建 App Widget。可以使用以下代码向用户请求授权: ```java // 请求授权 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, new ComponentName(context, MyWidgetProvider.class)); int REQUEST_PICK_APPWIDGET = 1; ((Activity) context).startActivityForResult(intent, REQUEST_PICK_APPWIDGET); ``` 6. 添加 App Widget桌面 可以使用以下代码将 App Widget 添加到桌面: ```java // 添加 App Widget Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); context.sendBroadcast(intent); ``` 其中,appWidgetIds 是 App Widget 的 ID 数组,可以在 onUpdate() 方法中获取。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值