2

What am trying to achieve is when a user clicks on widget it should take the user to the details of that particular task which is displayed on widget. I tried with following code but nothing is happening when clicking on widget. The details class(MasterList.class) requires data which needed to fetch from network. how do i achieve this ?

  1. Can i call network for data object from widget onUpdate(). Is this a bad practice ?

  2. Why there is no change nor errors in logcat ? Why app is not launching ??

  3. Did i made any blunder ??

    enter code here

    @Override 
     public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds)
    {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    Intent i = new Intent(context, CallNetwork.class);
    i.setAction(ACTION);
    context.startService(i); // starting service to get data from network
    
    
    Intent intent = new Intent(context, MasterListClass.class);
    while(CallNetwork.list.size()==0||CallNetwork.list.equals(null) //waiting for network call to complete
    
    {
    
    }if (CallNetwork.get(0).getName() != null) {
            this.list = CallNetwork.list;// getting data from network
        }
    intent.putExtra(context.getString(R.string.object),list.get(0));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
    RemoteViews views = new RemoteViews(context.getPackageName(), 
     R.layout.recipe_widget);
    views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds[0], views);}
    
Elletlar
  • 3,136
  • 7
  • 32
  • 38
user5444140
  • 83
  • 1
  • 10
  • I wouldn't recommend doing: "waiting for network call to complete" in the receiver 1. It is blocking the main thread 2. Waiting in a tight loop for network IO to complete is not a good practise. – Elletlar May 02 '18 at 09:21
  • What you could do is trigger an onUpdate() in the widget from the service once the data is ready. – Elletlar May 02 '18 at 09:23

1 Answers1

0

This code can be used to trigger an onUpdate() in the widget once the service has downloaded the data from the network:

Intent intent = new Intent(this, MyAppWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of 
AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = AppWidgetManager.getInstance(getApplication())
.getAppWidgetI‌​ds(new ComponentName(getApplication(), 
MyAppWidgetProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

Programmatically update widget from activity/service/receiver

Elletlar
  • 3,136
  • 7
  • 32
  • 38