1

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.

Community
  • 1
  • 1
Ali
  • 9,800
  • 19
  • 72
  • 152

1 Answers1

1

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() ...)

avimak
  • 1,628
  • 11
  • 18
  • I don't want to do that. That sentence was just an information with the link, but I wanted to see how to read it. – Ali Jul 05 '13 at 12:36
  • Oh, I see. There is more then one way to do it, but did you consider simply read the file and parse it? Take a look here: http://stackoverflow.com/questions/4329308/open-xml-file-from-res-xml-in-android – avimak Jul 05 '13 at 12:39
  • It means that I have to parse the xml file and there is no simpler code. is it what you meant? – Ali Jul 05 '13 at 12:44
  • No, you mustn't read the file itself, you can call `AppWidgetManager.getAppWidgetInfo()`, then read `AppWidgetProviderInfo.updatePeriodMillis` – avimak Jul 05 '13 at 12:52
  • It was a good point. But then it seems that I can get it with this code as you said (and I add as Addenda) within the Widget Class (extends AppWidgetProvider). `not in other activities.` but +1 for your guides till now. – Ali Jul 05 '13 at 13:16
  • Have you tried the following code from your Activity? `AppWidgetManager.getInstance()`, then use `AppWidgetManager.getAppWidgetIds()` to retrieve your widget(s) id, then continue as above (`AppWidgetManager.getAppWidgetInfo()` ...) – avimak Jul 05 '13 at 13:21
  • Yes, the codes are added at the end of my question, but returns NullPointerException. – Ali Jul 05 '13 at 13:24