1

I have an android widget which I want to update its own textview every 5 seconds.

Think of it as a news widget where the most important news will show on the widget one at a time. For demonstration purposes i will just use an array to show you the different texts.

The code is this:

public class ListingsWatchWidget extends AppWidgetProvider {

    private static int WIDGET_REFRESH_TIME = 5;
    public Handler handler = new Handler();

    //private RemoteViews views;    
    private int REFRESH_COUNT = 0;
    private String[] titles = {"Title 1", "Title 2", "Title 3"};

    public boolean killMe = false;

    private Context ctx;

    Runnable updateWidgetText = new Runnable()
    {
        @Override
        public void run() {

        Log.i("ListingsWatchWidget", "killMe is " + ListingsWatchWidget.this.killMe);
        if (ListingsWatchWidget.this.killMe) { return; }

        AppWidgetManager gm = AppWidgetManager.getInstance(ctx);                        
        ComponentName thisWidget = new ComponentName(ctx, ListingsWatchWidget.class);         

        int index = (int)(REFRESH_COUNT % titles.length);
        Log.i("ListingsWatchWidget", "index = " + index);

        RemoteViews views = new RemoteViews(ctx.getPackageName(),R.layout.widget);    
        views.setTextViewText(R.id.titleTextView, titles[index]);  

        gm.updateAppWidget(thisWidget, views);

        REFRESH_COUNT++;

        handler.postDelayed(this, WIDGET_REFRESH_TIME * 1000);

        }   
    };  


    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        Log.i("ListingsWatchWidget", intent.getAction());

        if (intent.getAction().equals("android.appwidget.action.APPWIDGET_ENABLED")) {
            handler.removeCallbacks(updateWidgetText);
            ctx = context;
            handler.postDelayed(updateWidgetText, WIDGET_REFRESH_TIME*1000);
        }
        else if (intent.getAction().equals("android.appwidget.action.APPWIDGET_DISABLED")) {
            handler.removeCallbacks(updateWidgetText);
            this.killMe = true;
        }

    }

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        //TODO ...
    }

}

Everything works as expected except when the user removes the widget from his desktop. The runnable continues to run endlessly.

I have tried various solutions suggests as this and this but none worked.

Let me give you a fragment if the logger when adding the widget to the desktop and when removing it:

06-20 22:22:57.318: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_ENABLED
06-20 22:22:57.328: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_UPDATE
06-20 22:23:02.323: I/ListingsWatchWidget(4012): killMe is false
06-20 22:23:02.323: I/ListingsWatchWidget(4012): index = 0
06-20 22:23:06.857: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_DELETED
06-20 22:23:06.877: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_DISABLED
06-20 22:23:07.338: I/ListingsWatchWidget(4012): killMe is false
06-20 22:23:07.338: I/ListingsWatchWidget(4012): index = 1

After the widget is deleted the runnable should had stop ...

Community
  • 1
  • 1
antoniom
  • 3,143
  • 1
  • 37
  • 53
  • This implementation will be unreliable. Android will terminate your process whenever it wishes, and you will no longer be updating your app widget. – CommonsWare Jun 20 '12 at 20:21
  • @CommonsWare What am doing wrong? Why removeCallbacks does not actually stops the Runnable ? – antoniom Jun 20 '12 at 20:24

0 Answers0