Question

I'm trying just some very simple code to get an Android widget going but with no luck. I've looked around everywhere and haven't found a good answer.

All I want (for now) is to increment a counter when the widget is touched and display the current value.

This is my AppWidgetProvider:

public class WordWidget extends AppWidgetProvider
{
    Integer touchCounter = 0;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        //This is run when a new widget is added or when the update period expires.
        Log.v("wotd",  "Updating " + appWidgetIds.length + " widgets");

        for(int x = 0; x < appWidgetIds.length; x++)
        {
            Integer thisWidgetId = appWidgetIds[x];

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            remoteViews.setTextViewText(R.id.mainText, touchCounter.toString());

            Intent widgetIntent = new Intent(context, WordWidget.class);
            widgetIntent.setAction("UPDATE_NUMBER");

            PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);

            remoteViews.setOnClickPendingIntent(R.id.widgetLinearLayout, widgetPendingIntent);
            appWidgetManager.updateAppWidget(thisWidgetId, remoteViews);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.v("wotd", "In onReceive with intent=" + intent.toString());
        if (intent.getAction().equals("UPDATE_NUMBER"))
        {
            Log.v("wotd", "In UPDATE_NUMBER");
            touchCounter++;
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            remoteViews.setTextViewText(R.id.mainText, touchCounter.toString());
        } else
        {
            Log.v("wotd", "In ELSE... going on to super.onReceive()");
            super.onReceive(context, intent);
        }
    }
}

This is part of my manifest:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <receiver
        android:icon="@drawable/ic_launcher"
        android:name="com.example.mywidget.WordWidget"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="UPDATE_NUMBER" />
        </intent-filter>

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

</application>

The log shows the onReceive() is called immediately after being placed on the home screen, and after being touched, however the number never increases. I don't totally understand how widgets work, but are they killed after onUpdate()? So to do this I would have to use some kind of persistant storage?

Also, if I currently add another widget both would show the same values and increment even if I just touch one. Is there a way for each and any widget to have it's own counter?

Thanks!

Was it helpful?

Solution

Actually you have answered your question. But let's clarify some things:

A. AppWidgets are NOT killed as long as they are on a home screen. But they don't belong to you. They are running in the process of the home application. To be more specific their views are running in the process of the home application, this is why you are see your widget but it doesn't do what you are expected and this is why we are using RemoteViews instead of Views in order to update them.

B. AppWidgetProviders (in your case the WordWidget class), on the other hand, are destroyed as soon as the onReceive method finishes. All the variables in them are re-initialized every time the onReceive method gets called. This is why your number never increases. The purpose of an AppWidgetProvider is to update the widget's RemoteViews and to inform your application that a registered broadcast has arrived.

C. AppWidgetProvider's onUpdate method provides you an Array with the widgets Ids that must be updated. This is the only code point you can use to get the number of your widget instances and their Ids. Because of the RemoteViews there is NO way to get some useful value from the Views of your widget (for example you can NOT read the counter value from the TextView) so you must use the provided information and DO persist your counter values per widget id. When the next onUpdate gets called you read the value from the storage, increase it, update the widget with the new value and then store the new value back.

D. If your widget has to do many things or slow things (like networking) when its time to update itself, you should consider using a service for this. But in your case (to increase a number) your approach is just fine as long as you persist the counters in the persistent storage or somewhere else.

Finally I 've noticed that in your onReceive override you are calling the "super.onReceive" only if you don't receive the "UPDATE_NUMBER" action. This is NOT a good practice, unless there is a GOOD reason (that's another story) always call super.onReceive as the first or the last command in your override.

Hope this helps...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top