Question

I just created my first ever widget and it all went well until I got to the part where I'm supposed to set when it updates. Here's my code to make my question clear:

Android Manifest:

<receiver android:name="com.whizzapps.stpsurniki.widget.UrnikWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/app_widget_info"/>
    </receiver>

App Widget Provider XML:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="80dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="10"
    android:initialLayout="@layout/app_widget"
    android:widgetCategory="home_screen" >
</appwidget-provider>

AppWidgetProvider class:

public class UrnikWidget extends AppWidgetProvider {

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

    Log.i("TAG", "onUpdate");
    }
}

I have created a simple widget with a TextView in it that displays a certain word, it has no logic at all yet since I'm still learning how to work with widgets. As you can see I made a log.i to see when onUpdate is actually called and my problem is that it is only called the first time I put the widget on my home screen.

I know that update interval of 10 milliseconds is too low and should not be used because of battery drain but I'M USING 10ms just for testing purposes.

Why does onUpdate only executes once instead of every 10ms? And what would be the best way for me to update widget if I need it updated at certain hours only? Let's say I only need my widget to be updated at 7AM, 8AM and 3PM. Is this possible? Thank you!

Was it helpful?

Solution

Yes it is very bad idea to update widget using 10 milliseconds interval.

I have got the same problem as you and try many different solution.

If you want frequently update your widget there is two solution:

  1. Use AlarmMenager and next run Intent every interval millis
  2. Use Service and update RemoteView

The first one is very good when you create clock and you need 1 second interval. But when you need interval < 1 second its not god - problem with delay intent. For your purpose use Service, RemoteView is not a standard view the prons is that you can update that view from any place, this is a code:

public class WidgetService extends Service {

private ScheduledExecutorService scheduleTaskExecutor;

public WidgetService() {
}

@Override
public void onDestroy() {
    super.onDestroy();
    scheduleTaskExecutor.shutdownNow();
    scheduleTaskExecutor = null;
}

@Override
public void onCreate() {
    super.onCreate();
    scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    final Context cx = this;
    final long startTime = SystemClock.uptimeMillis();
    scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
           //logic here for example
           RemoteViews rv = new RemoteViews(this.getPackageName(), getLayoutId());

        }
    }, 0, 10, TimeUnit.MILLISECONDS);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

Be careful with onUpdate method there is a very serious bug and onUpdate method is called even your widget is not visible so android create phantom widgets, http://code.google.com/p/android/issues/detail?id=2539,

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