简体   繁体   English

Android小部件的固定更新频率

[英]Android widget's fixed update frequency

In order to have Widget , we need to maintain an XML file (AppWidgetProviderInfo) which describes the properties of the widget, eg size or fixed update frequency like this: 为了拥有Widget ,我们需要维护一个XML文件(AppWidgetProviderInfo),该文件描述了Widget的属性,例如大小或固定的更新频率,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="160dp" 
    android:minHeight="72dp"
    android:initialLayout="@layout/widget_layout"
    android:updatePeriodMillis="1800000"
    android:configure="android.project.WidgetConfigure" >

</appwidget-provider>

and Maintain the App Widget configuration in the AndroidManifest.xml. 并在AndroidManifest.xml中维护App Widget配置。

<meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/widget_info" />
 </receiver>

I know that we can not edit the updatePeriodMillis . 我知道我们无法编辑updatePeriodMillis programmatically and we need to use AlarmManager. 以编程方式,我们需要使用AlarmManager。 ( Android Widget set android:updatePeriodMillis programmatically ) Android Widget以编程方式设置android:updatePeriodMillis

But how can I read that value from the xml file programmatically. 但是,我如何以编程方式从xml文件读取该值。 Is there any way ? 有什么办法吗?

Addenda: 附加物:

try {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
        AppWidgetProviderInfo info = appWidgetManager.getAppWidgetInfo(R.xml.widget_info);
        System.out.println("Time: " + info.updatePeriodMillis);
    } catch(Exception err) {
        err.printStackTrace();
    }

If I use this code in an Activity, it returns NullPointerException in catch. 如果我在Activity中使用此代码,它将在catch中返回NullPointerException。

updatePeriodMillis is a value which the system uses in order to send you a proper broadcast once that period's time has come. updatePeriodMillis是一个值,系统会使用该值,以便在该时间段到来后向您发送适当的广播。 Why will you want to read it and trigger an AlarmManager intent for the exact same time? 为什么要读取它并在同一时间触发AlarmManager意图?

After your edit: You are passing the wrong "id"; 编辑后:您传递的是错误的“ id”; you should pass the actual widget id, not its XML metadata id. 您应该传递实际的小部件ID,而不是其XML元数据ID。 As I wrote in my last comment, try this: AppWidgetManager.getInstance() , then use AppWidgetManager.getAppWidgetIds() to retrieve your widget(s) id, then continue as discussed ( AppWidgetManager.getAppWidgetInfo() ...) 正如我在最后一条评论中所写,请尝试以下操作: AppWidgetManager.getInstance() ,然后使用AppWidgetManager.getAppWidgetIds()来检索您的小部件ID,然后按照讨论继续操作( AppWidgetManager.getAppWidgetInfo() ...)

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

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