title: Android Widget - update when a button is clicked
body: <p>Good day.
  I'm trying to create a widget that will show the balance on the screen.
  Created 2 buttons and set up the listener to determine which button was pressed.</p>
  
  <p><strong>Difficulty:
  How to update data by clicking on the button or reload the widget so that the data is updated?</strong></p>
  
  <pre><code>import android.app.AlarmManager;
  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.util.Log;
  import android.widget.RemoteViews;
  import android.widget.Toast;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.text.DateFormat;
  import java.util.Date;
  
  public class EthWidgetProvider extends AppWidgetProvider {
      private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";
      private static final String SYNC_CLICKED2    = "automaticWidgetSyncButtonClick2";
  
  
      static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
          RequestSingleton.getInstance(context).fetchData(new VolleyCallback() {
              @Override
              public void onSuccessRequest(JSONArray result) {
                  Log.i("Response", result.toString());
                  String price = "";
                  try {
                      JSONObject etherObject = result.getJSONObject(0);
                      price = etherObject.getString("price_usd");
                  } catch (JSONException e) {
                      Log.e("JSONException", e.toString());
                  }
                  Log.i("Price", price);
                  RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_widget);
                  views.setTextViewText(R.id.gprs, "$" + price);
  
                  String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
                  views.setTextViewText(R.id.timeofday, currentDateTimeString);
  
                  appWidgetManager.updateAppWidget(appWidgetId, views);
              }
          });
      }
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  
  
          RemoteViews remoteViews;
          ComponentName watchWidget;
  
          remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widget);
          watchWidget = new ComponentName(context, EthWidgetProvider.class);
  
          remoteViews.setOnClickPendingIntent(R.id.refresh, getPendingSelfIntent(context, SYNC_CLICKED));
          remoteViews.setOnClickPendingIntent(R.id.example_widget_button, getPendingSelfIntent(context, SYNC_CLICKED2));
          appWidgetManager.updateAppWidget(watchWidget, remoteViews);
  
          for(int appId : appWidgetIds) {
              updateAppWidget(context, appWidgetManager, appId);
          }
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          super.onReceive(context, intent);
  
          if (SYNC_CLICKED.equals(intent.getAction())) {
  
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  
              RemoteViews remoteViews;
              ComponentName watchWidget;
  
              remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widget);
              watchWidget = new ComponentName(context, EthWidgetProvider.class);
  
              remoteViews.setTextViewText(R.id.timeofday, "TESTING");
  
              appWidgetManager.updateAppWidget(watchWidget, remoteViews);
  
          }
  
          if (SYNC_CLICKED2.equals(intent.getAction())) {
  
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  
              RemoteViews remoteViews;
              ComponentName watchWidget;
  
              remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widget);
              watchWidget = new ComponentName(context, EthWidgetProvider.class);
  
              remoteViews.setTextViewText(R.id.timeofday, "TESTING2");
  
              appWidgetManager.updateAppWidget(watchWidget, remoteViews);
  
          }
      }
  
  protected PendingIntent getPendingSelfIntent(Context context, String action) {
          Intent intent = new Intent(context, getClass());
          intent.setAction(action);
          return PendingIntent.getBroadcast(context, 0, intent, 0);
      }
  }
  </code></pre>
  
  <p>By pressing button 1, I need to show the activity. By pressing the second button, you need to update the widget so that the data is updated, that is, so that the request to the server goes again and the data is updated.</p>
  
  <p>Any help would be appreciated.</p>