Ali
Ali

Reputation: 9994

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, e.g. size or fixed update frequency like this:

<?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.

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

I know that we can not edit the updatePeriodMillis. programmatically and we need to use AlarmManager. (Android Widget set android:updatePeriodMillis programmatically)

But how can I read that value from the xml file programmatically. 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.

Upvotes: 1

Views: 841

Answers (1)

avimak
avimak

Reputation: 1628

updatePeriodMillis is a value which the system uses in order to send you a proper broadcast once that period's time has come. Why will you want to read it and trigger an AlarmManager intent for the exact same time?

After your edit: You are passing the wrong "id"; you should pass the actual widget id, not its XML metadata 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() ...)

Upvotes: 1

Related Questions