Automatically update widget in the home screen when i make modification in my application

无人久伴 提交于 2019-12-23 03:22:41

问题


How can we updating the View of a Home Screen Widget on the onReceive method of AppWidgetProvider?.I am trying to update the listview of my Home screen widget but it seems that I cant access the listview of my AppWidgetProvider on onReceive method.My problem is when i changes in my application and come back to the home screen that the widget has no modification.Here is a sample code of my onReceive

public class WidgetTaskSchedular extends AppWidgetProvider {
private final String TAG = "CalendarViewSample:"
        + this.getClass().getName();
static int ID;
int[] sameid=new int[1]; 

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if(intent.getAction().equals("update_widget"))
    {
    Log.i(TAG,"AppWidgetIds:"+ID);

    for(int i=0;i<1;i++)
    {
        sameid[i]=ID;
        Log.i(TAG,"SameId:"+sameid[i]);
        onUpdate(context, AppWidgetManager.getInstance(context),sameid);
    }

    }
}
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++) {
          ID=appWidgetIds[i];
        Log.i(TAG,"LengthofWidget:"+appWidgetIds.length);
          Log.i(TAG,"TestAppWidget:"+appWidgetIds[i]);
          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);
        }

}

}

MainActivity class: In this main class i change some modification and broadcast this to widget class using on pause method:

@Override
public void onPause() {
    super.onPause();
    Intent updateWidget = new Intent(getActivity(), WidgetTaskSchedular.class);
    updateWidget.setAction("update_widget");
    PendingIntent pending = PendingIntent.getBroadcast(getActivity(), 0, updateWidget, PendingIntent.FLAG_CANCEL_CURRENT);
    try {
        pending.send();
    } catch (CanceledException e) {
        e.printStackTrace();
    }
}

来源:https://stackoverflow.com/questions/18376263/automatically-update-widget-in-the-home-screen-when-i-make-modification-in-my-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!