OnUpdate NEVER called - Android Widget

I ran various widget tutorials like this one and this one .

I tried to adapt my code to my goals, and I tried to copy the copied copy. It seems that no matter what I do, my widget is NEVER updated. When it is placed on the main screen, the text remains in the form of static text with which it was created. All I need for this application is to update the 4 TextViews that will be contained in the layout.

The code below is mostly copied from one of the tutorials. I tried to debug the onUpdate method, however the breakpoint never hits.

Any help would be greatly appreciated.

EDIT: I went back to a much simpler version of this widget, which I tried in my efforts, and replaced the code below with the code of a simpler widget. I made the changes suggested by CommonsWare to my manifest. Unfortunately, the problem remains.

My main .java file looks like this:

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 information:

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

+6
source share
1 answer

Your manifest is wrong. You claim that you have a class network.widget.AppWidgetProvider and AFAIK, you do not. You have a class strangely named network.widget.NetworkWidgetActivity . You should use this in your <receiver> element.

also:

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

  • If you plan to do more serious work in the service (database I / O, network I / O, etc.), consider simply moving all this logic to onUpdate() and getting rid of the service, since it is not very buys you a lot here (and if you are going to save the service, switch to IntentService and get rid of stopSelf() , because it is being processed for you)

  • While you are requesting 10 second updates, the minimum effective updatePeriodMillis is 30 minutes - just keep this in mind when debugging

  • int[] widgetId doesn't seem to be used in NetworkWidgetActivity

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

+7
source

Source: https://habr.com/ru/post/910949/


All Articles