Question

I am trying to create a LinearLayout based Widget which will refresh itself with stock ticker symbol and Price after every few minutes ( configurable by users in Configuration Activity ).

Here are the steps I follow :

1) Create the Remote Service in a separate process ( I have tested the service and works fine) 2) Implement the Widget as per below code snippet.

My question is, how should one design the Widget which refresh itsellf from data coming from remote service ?

(1) Using my below approach - start/bind to Remote service from TimerTask and update widget after every few minutes ( Trying to test it or every 5 second for test purpose only...so please bear with me ....Not working by the way)

(2) Should I simply declare the custom broadcast filter in my app Widget (since essentially its a BroadCastReceiver) and override the onReceive and publish the data from the extras from intent received from remote service ( My Remote Service can broadcast the custom intent every 0 second)

What is the best option available? I tried to search for a example scenario where someone is having similar issue like mine but could not find much information.

Here is the code for my widget -

// Author - Anand M
public class TestWidget extends AppWidgetProvider {

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

        for (int i = 0; i < N; i++) {
            // We have only one widget on Home Screen
            int appWidgetId = appWidgetIds[i];
            timer.scheduleAtFixedRate(new MyRefreshTimer(context, appWidgetManager, appWidgetId), 3000, 5000);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        Log.d(LOGTAG, "onDeleted");

        timer.cancel();
        timer = null;
    }

    @Override
    public void onEnabled(Context context) {
        Log.d(LOGTAG, "onEnabled");

    }

    @Override
    public void onDisabled(Context context) {
        Log.d(LOGTAG, "onDisabled");
    }

    private class MyRefreshTimer extends TimerTask {
        // Remote Service Interface declaration
        // Connection to Remote Service

        public MyRefreshTimer() {

            // Initialize it here
            // Start and Bind to Remote Service


        }

        public void run() {

            // get data from Remote Service 
            // if the data received is not null then Push it to this widget 
            ComponentName thisWidget = new ComponentName(_ctx, TestWidget.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(_ctx);
            manager.updateAppWidget(thisWidget, remoteViews);

            Intent myWidgetIntent = new Intent(_ctx, TestWidget.class);
            myWidgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
            _ctx.sendBroadcast(myWidgetIntent);

        }

    }
}

Additional Info :

(1)I have declared the widget_provider layout,

(2) widget_provider_info Layout

(3) and declared the widget as a receiver ( as per documents : URL:http://developer.android.com/guide/topics/appwidgets ).

Was it helpful?

Solution

  1. Implement LocalService in the widget class
  2. Implement TimerTask in the Widget class
  3. Start LocalService from run() method of TimerTask
  4. LocalService will refresh the widget view by fetching data from remote service
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top