Skip to content Skip to sidebar Skip to footer

How To Perform Widget Update By Service After Clicking A Button?

This is my Configure activity when I try to deploy the widget initially. public class WeatherAppWidgetConfigure extends PreferenceActivity{ private List weat

Solution 1:

It must be a problem with this line in your widget provider

PendingIntentpendingIntent= PendingIntent.getService(context.getApplicationContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

Most likely a problem with "context.getApplicationContext()" Try using a few different values, such as "Context", "context", "getBaseContext()", or "this". I'm not exactly sure which is correct, in a similar situation in my app I use "context", which works for me.

Also try changing

Intent intent = newIntent(context.getApplicationContext(),
                                    UpdateWeatherAppWidgetService.class);

to:

 Intent intent = newIntent(context, UpdateWeatherAppWidgetService.class);

Solution 2:

Hey i am in your android course here in Vaxjo:D For your question: I solved that in the AppWidgetProvider and then "transfered it to a service" with some minor changes.

privatestaticfinalStringWIDGET_BUTTON="com.example.ass3.WIDGET_BUTTON";

@OverridepublicintonStartCommand(Intent intent,int flag, int startId) {


    System.out.println("inService");
    AppWidgetManagerappWidgetManager= AppWidgetManager.getInstance(this
            .getApplicationContext());

     RemoteViewsviews=newRemoteViews(this.getApplicationContext().getPackageName(), R.layout.main);
     ComponentNamewatchWidget=newComponentName(this.getApplicationContext(), WeatherWidget.class);

       views.setOnClickPendingIntent(R.id.widget_refresh, myIntent( WIDGET_BUTTON,this.getApplicationContext()));
       appWidgetManager.updateAppWidget(watchWidget, views);





        stopSelf(startId);

    return START_STICKY;

  }

 protected PendingIntent myIntent(String button,Context context) {
        Intentintent=newIntent(context, WeatherWidget.class);
        intent.setAction(button);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

And in your Widget Provider

@OverridepublicvoidonReceive(Context context, Intent intent) {
            super.onReceive(context, intent);

            //Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();//System.out.println("Intent get Action"+ intent.getAction());if (WIDGET_BUTTON.equals(intent.getAction())) {

             AppWidgetManagerappWidgetManager= AppWidgetManager.getInstance(context);

               RemoteViewsremoteViews=newRemoteViews(context.getPackageName(), R.layout.main);
               ComponentNamewidget=newComponentName(context, WeatherWidget.class);


                remoteViews.setTextViewText(R.id.widget_curtemp, "20");
                remoteViews.setTextViewText(R.id.widget_minmaxtemp, "10/30");  
                remoteViews.setImageViewResource(R.id.widget_image, R.drawable.sun);
                remoteViews.setTextViewText(R.id.widget_city, "vaxjo");
                remoteViews.setTextViewText(R.id.widget_desc,"trying to refreshs");
                appWidgetManager.updateAppWidget(widget, remoteViews);
             }

     }

And permission for service and permission to receive broadcast in Manifest. Please don't judge my hardwired values that i update the boxes , i am still trying to find out how to fetch the data properly :D

Post a Comment for "How To Perform Widget Update By Service After Clicking A Button?"