Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnUpdate NEVER called - Android Widget

I have been following various Widget Tutorials such as this one and this one

I have tried adapting their code to my purposes, and I have tried straight-up copy paste. It seems no matter what I do, my widget NEVER gets updated. When it is placed on the home screen the text stays as the Static Text it was created with. All I need this app to do is update 4 TextViews which will be contained in the layout.

The code below is mostly copied from one of the tutorials. I've tried debugging the onUpdate method, however the break point never seems to be hit.

Any help would be GREATLY appreciated.

EDIT: I reverted to a much simpler version of this widget that I was trying earlier in my efforts and replaced the code below with the code for the simpler widget. I made the changes suggested by CommonsWare to my manifest. Unfortunately the problem remains.

My main .java file looks like so:

public class NetStatWidget extends AppWidgetProvider 
{

public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds)
{   
    ComponentName thisWidget = new ComponentName(context, NetStatWidget.class);
    int[] widgetId = manager.getAppWidgetIds(thisWidget);

    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.appwidget);
    remoteView.setTextViewText(R.id.textView0, "Hello");
    manager.updateAppWidget(widgetId, remoteView);
}

}

My Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.stat"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

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


            <receiver android:name="NetStatWidget" >
                <intent-filter>
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                </intent-filter>
                <meta-data 
                    android:name="android.appwidget.provider"
                    android:resource="@xml/providerinfo" />
            </receiver>

    </application>

</manifest>

And my Widget Provider Info:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/main"
android:minWidth="146dip"
    android:minHeight="72dip"
    android:updatePeriodMillis="10000">

like image 762
Jodron Avatar asked Mar 16 '12 20:03

Jodron


1 Answers

Your manifest is wrong. You are claiming that you have a network.widget.AppWidgetProvider class, and AFAIK, you don't. You have an oddly-named network.widget.NetworkWidgetActivity class. You need to use that in your <receiver> element.

Also:

  • Replace this.getApplicationContext() and getApplicationContext() with this

  • Unless you plan on doing more serious work in the service (database I/O, network I/O, etc.), consider just moving all that logic into onUpdate() and getting rid of the service, since it's not really buying you much here (and if you are going to keep the service, switch to an IntentService and get rid of stopSelf() since that's handled for you)

  • While you are asking for 10-second updates, the minimum effective updatePeriodMillis is 30 minutes -- just keep that in mind while you are debugging

  • int[] widgetId seems to be unused in NetworkWidgetActivity

  • onStart() in a Service has been deprecated for a couple of years; use onStartCommand() instead

like image 194
CommonsWare Avatar answered Oct 10 '22 03:10

CommonsWare