Pergunta

My friend have completed one application and now he created one widget for his application which can be used to display the list of task in list view manner. In first when he drag the widget and install in home screen it will be working fine as well as display the task in list view manner.

His problem is, when he made changes in his application and then come back to his home screen, No changes reflected. He is using onReceive() method for this one to update the widget

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

public class WidgetTaskSchedular extends AppWidgetProvider {
    private final String TAG = "CalendarViewSample:"
            + this.getClass().getName();

    SharedPreferences sharedprefer;

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if(intent.getAction().equals("update_widget"))
        {
            int[] appid=new int[1];
            sharedprefer=context.getSharedPreferences("TaskWidget", 0);
            appid[0]=sharedprefer.getInt("widget_key",0);
            Log.i(TAG,"Appwidgetid"+appid[0]);
            onUpdate(context, AppWidgetManager.getInstance(context),appid);

        }
    }
    public static String EXTRA_WORD=
            "com.capsone.testing.calendar.WORD";
    @SuppressWarnings("deprecation")
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
         for (int i=0; i<appWidgetIds.length; i++) {

             sharedprefer=context.getSharedPreferences("TaskWidget", 0);
             SharedPreferences.Editor editor=sharedprefer.edit();
             editor.putInt("widget_key", appWidgetIds[i]);
             editor.commit();
             int a= sharedprefer.getInt("widget_key", 0);
             Log.i(TAG,"Sharedpreferencewidgetid:"+a);

              //ID=appWidgetIds[i];
              Log.i(TAG,"LengthofWidget:"+appWidgetIds.length);
             // Log.i(TAG,"TestAppWidget:"+ID);
              Intent intentWidgetService=new Intent(context, WidgetService.class);
              intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
              intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME)));

              RemoteViews remoteView=new RemoteViews(context.getPackageName(),
                                                  R.layout.widgetlayout);

              remoteView.setRemoteAdapter(appWidgetIds[i], R.id.listWidget,
                                      intentWidgetService);

              Intent clickIntent=new Intent(context, ActionBarActivity.class);
              PendingIntent clickPendingIntent=PendingIntent
                                      .getActivity(context, 0,
                                                    clickIntent,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
              remoteView.setPendingIntentTemplate(R.id.listWidget, clickPendingIntent);
             ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
              appWidgetManager.updateAppWidget(component, remoteView);
            }

    }
}
Foi útil?

Solução

I suggest you to try the code pasted below:

ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listWidget);
appWidgetManager.updateAppWidget(component, remoteView);

notifyAppWidgetViewDataChanged() --->Notifies the specified collection view in all the specified AppWidget instances to invalidate their currently data.

So when you call notifyAppWidgetViewDataChanged() then the onDataSetChanged() method of RemoteViewsFactory will be called which is serving as a Adapter for your ListView. You can do web-service/network related operations, data fetching operation from database and other operations in onDataSetChanged(), get the response and add it to your data-set which maybe ArrayList or any such Collection.

You can refer Using App Widgets with Collections

Outras dicas

Have you tried the solution mentioned here:

How to update a Widget dynamically (Not waiting 30 min for onUpdate to be called)?

Please note that the above just triggers the update, the widget itself will need to fetch the required data.

Please be aware that while broadcast receivers CAN be registered in a widget, after an update, they are up for garbage collection and, as such, not a reliable solution.

A final alternative would be to register an AlarmManager to fire a Service performing the widget updates.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top