nfc-uk
nfc-uk

Reputation: 696

Android broadcast receiver not firing up for updating AppWidget

I am trying to test my app Widget and have registered the receiver in the manifest as follows:

    <receiver android:name=".MyUtilityWidget" android:label="@string/widget_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config" />
    </receiver>

The MyUtility code:

     public class MyUtilityWidget extends AppWidgetProvider {

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

           my Main Code here.... (widget stuff)
   }

My widget starts and executes once fine (hence I leave the code out of here). However the broadcast receiver is not working and I receive no broadcasts. In fact LogCat shows the intent is never executed. So, my code block only executes once on apk deployment and never again (widget text shows, but never updates every 30m as I have set).

Config here...

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget_message"
/>

Also updatePeriodMillis cannot be less than 30 minutes? This is a very annoying issue when debugging, as I have to wait around 30 minutes each run to check if the intent fired!

Any help appreciated.

Upvotes: 0

Views: 761

Answers (1)

LuxuryMode
LuxuryMode

Reputation: 33741

You need to grab hold of your widget via RemoteViews and update it accordingly. It doesn't just update automatically.

Try something like this:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
                .getApplicationContext());
        ComponentName thisWidget = new ComponentName(getApplicationContext(),
                MyWidgetProvider.class);
        int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
        final int N =allWidgetIds2.length;
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = allWidgetIds2[i];
            RemoteViews widgetView = new RemoteViews(getPackageName(), R.layout.widget_layout);
            widgetView.setTextViewText(R.id.some_text_view, "some text");

Upvotes: 1

Related Questions