Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnUpdate() not calling the widget service

i am developing a widget for my app. Widget name is history, when user browses any URL i will store it in local db, which widget listing from it.

when i open the widget, it fetches all the details from the local db and showing it in widget's list-view.

1) when i open the widget, first onUpdate method of WidgetProvider is called

2) then onUpdate() will call the service WidgetService

3) then service will call the Widget Factory to update the list-view using locally stored db.

In widget factory it will get the db values and update the lisviews in widget.

the above is the process happening when setting up the widget.

The first class:

        public class WidgetProviderFavorite extends AppWidgetProvider {

        @SuppressWarnings("deprecation")
        @Override
          public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,int[] appWidgetIds) {            
            for (int i=0; i<appWidgetIds.length; i++) {
              Intent svcIntent=new Intent(ctxt, WidgetServiceFavorite.class);
              svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
              svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));  
              RemoteViews widget=new RemoteViews(ctxt.getPackageName(),R.layout.widget_favorite);
              widget.setRemoteAdapter(appWidgetIds[i], R.id.listFavoriteLogin,svcIntent);                                  
              Intent clickIntent=new Intent(ctxt, WidgetActivityFavorite.class);                                 
              PendingIntent clickPI=PendingIntent.getActivity(ctxt, 0,clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);

              widget.setPendingIntentTemplate(R.id.listFavoriteLogin, clickPI);                                              
              appWidgetManager.updateAppWidget(appWidgetIds[i], widget);

            }

            super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
          } 

The second class:

        public class WidgetServiceFavorite extends RemoteViewsService {
          @Override
          public RemoteViewsFactory onGetViewFactory(Intent intent) {

            return(new WidgetFactoryFavorite(this.getApplicationContext(),intent));         
          }
        }

The third class: which is going to fetch and update the listview.

fetching done in construtor and getViewAt() (Its called automatically) will be setting the values.

        public class WidgetFactoryFavorite extends Activity implements RemoteViewsService.RemoteViewsFactory {

          public WidgetFactoryFavorite(Context ctxt, Intent intent) {
              int array_Index=0;
              int array_Length=0;
              WidgetDatabase db1=new WidgetDatabase(ctxt);
              List<WidgetDatabaseModel> facorite = db1.getFavorite();
              array_Length=facorite.size(); 

              mNames=new String[array_Length];                                     
              mSummary=new String[array_Length];

              for (WidgetDatabaseModel cn : facorite) {                     
                  mNames[array_Index] =cn.getNAME(); 
                  mSummary[array_Index]= cn.getSUMMARY();
                  array_Index++;
              } 

              this.mCtxt=ctxt;
              intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                              AppWidgetManager.INVALID_APPWIDGET_ID);

          }
            @Override
          public void onCreate() {
            // no-op
          }

          @Override
          public void onDestroy() {
            // no-op
          }

          @Override
          public int getCount() {
            return(mNames.length);
          }

          @Override
          public RemoteViews getViewAt(int position) {

              RemoteViews row=new RemoteViews(mCtxt.getPackageName(),
                                             R.layout.widget_rowfavorite);

              row.setTextViewText(android.R.id.text1, mNames[position]);

              Intent i=new Intent();
              Bundle extras=new Bundle(); 
              extras.putString(WidgetProviderFavorite.sExtra_Guid, mGuid[position]);
              extras.putString(WidgetProviderFavorite.sExtra_Url, mUrl[position]);
              i.putExtras(extras);
              row.setOnClickFillInIntent(android.R.id.text1, i);

              return(row);
          }

          @Override
          public RemoteViews getLoadingView() {
            return(null);
          }

          @Override
          public int getViewTypeCount() {
            return(1);
          }

          @Override
          public long getItemId(int position) {
            return(position);
          }

          @Override
          public boolean hasStableIds() {
            return(true);
          }

          @Override
          public void onDataSetChanged() {
          }
        }

My Problem is:

Its not syncing side by by side when i browse some URL in my browser. i have used the below code sync when database is updated by new URL.

        int[] ids = AppWidgetManager.getInstance(mActivity).getAppWidgetIds(new ComponentName(mActivity, WidgetProviderFavorite.class));
        WidgetProviderFavorite myWidget = new WidgetProviderFavorite();
        myWidget.onUpdate(mActivity, AppWidgetManager.getInstance(mActivity),ids);

its calling the onUpdate method(). but onUpdate doesn't call the service class which is going to call factory class and upadting the lisview with new database values. the flow i mentioned earlier was not happening when new values added to db.

why service method is not called?

or any other method refresh the list-view when database values changes

like image 581
Vji Avatar asked Feb 09 '17 06:02

Vji


People also ask

Why onupdate is not called when widget update?

Because if you do, then onUpdate is not called and it is the job of the configuration activity to manually update the widget for the first time. After that onUpdate should be called as defined in your updatePeriod configuration.

What is the use of onupdate () method?

The onUpdate () method is responsible for updating your widget’s Views with new information. If you open your project’s widget provider ( java/values/NewAppWidget.java) then you’ll see that it already contains the outline of an onUpdate () method:

How do I update a widget?

Regardless of whether your widget updates automatically or in response to user interaction, the update process is exactly the same: the widget manager sends a broadcast intent with the ACTION_APPWIDGET_UPDATE action, and the widget-provider class responds by calling the onUpdate () method, which in turn calls the updateAppWidget () helper method.

How to fix widget not responding on Android?

Step 1: Open the Settings app on your phone. Navigate to Apps and notifications. Step 2: Now scroll down and find the app associated with the widget from the list. Step 3: Go to Storage and cache and tap on the Clear cache button to remove cache data. Now the app should purge any cache data and load up things from scratch.


1 Answers

Answering my self:

We have to call notifyAppWidgetViewDataChanged() in onUpdateMethod().

   appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listView);

Complete OnUpdate():

    public class WidgetProviderFavorite extends AppWidgetProvider {

    @SuppressWarnings("deprecation")
    @Override
      public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,int[] appWidgetIds) {            
        for (int i=0; i<appWidgetIds.length; i++) {
          Intent svcIntent=new Intent(ctxt, WidgetServiceFavorite.class);
          svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
          svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));  
          RemoteViews widget=new RemoteViews(ctxt.getPackageName(),R.layout.widget_favorite);
          widget.setRemoteAdapter(appWidgetIds[i], R.id.listFavoriteLogin,svcIntent);                                  
          Intent clickIntent=new Intent(ctxt, WidgetActivityFavorite.class);                                 
          PendingIntent clickPI=PendingIntent.getActivity(ctxt, 0,clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);

          widget.setPendingIntentTemplate(R.id.listFavoriteLogin, clickPI); 

          appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listView);                

          appWidgetManager.updateAppWidget(appWidgetIds[i], widget);

        }

        super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
      } 
like image 115
Vji Avatar answered Oct 06 '22 00:10

Vji