title: How do I run an Activity from a button on a widget in Android?
body: <p>I'm working on a toggle widget that consists of a button. When pressed, I'd like it to run an activity without opening up anything (just saying on the desktop as usual). Is there a way to directly run an activity from a button press on a desktop widget? Thanks!
  UPDATE: Now I'm trying to toggle silent mode from within the code without running a new activity.
  Here is my current code (the buttons do nothing when I click them for some reason)</p>
  
  <pre><code>package toggleHD.widget;
  
  import android.app.Activity;
  import android.app.IntentService;
  import android.app.Notification;
  import android.app.NotificationManager;
  import android.app.PendingIntent;
  import android.media.AudioManager;
  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;
  
  
      public class ButtonWidget extends AppWidgetProvider {
  
          public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget";
  
          public static int appid[];
          public static RemoteViews rview;
          @Override
          public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
                  int[] appWidgetIds){
              updateWidgetState(context, ""); 
          }
          @Override
          public void onReceive(Context paramContext, Intent paramIntent)
            {
               String str = paramIntent.getAction();
              if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) {
                  updateWidgetState(paramContext, str);   
              }
              else
              {
                      if ("android.appwidget.action.APPWIDGET_DELETED".equals(str))
                        {
                          int i = paramIntent.getExtras().getInt("appWidgetId", 0);
                          if (i == 0)
                          {
  
                          }
                          else
                          {
                              int[] arrayOfInt = new int[1];
                              arrayOfInt[0] = i;
                              onDeleted(paramContext, arrayOfInt);
                          }
                        }
                super.onReceive(paramContext, paramIntent);
              }
            }
           static void updateWidgetState(Context paramContext, String paramString)
            {
              RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
              ComponentName localComponentName = new ComponentName(paramContext, ButtonWidget.class);
              AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
            }
           private static RemoteViews buildUpdate(Context paramContext, String paramString)
            {
              // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
              rview = new RemoteViews(paramContext.getPackageName(), R.layout.main);
              Intent active = new Intent(paramContext, ButtonWidget.class);
              active.setAction(ACTION_WIDGET_CLICK_RECEIVER);
  
             // PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);
  
  // upadte this R.id.buttonus1 with your layout or image id on which click you want to start Activity
  
  Intent configIntent = new Intent(paramContext, TestReceiver.class);
  configIntent.setAction(ACTION_WIDGET_CLICK_RECEIVER);
  PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, configIntent, 0);
              if(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER))
              {
  
                 //open Activity here..
               //your code for update and what you want on button click
  //
           rview.setOnClickPendingIntent(R.id.button_one, configPendingIntent);
              }  
               return rview; 
            }
          @Override
          public void onEnabled(Context context){
              super.onEnabled(context);
             // Toast.makeText(context, "onEnabled()  ", Toast.LENGTH_SHORT).show();
          }
          // Called each time an instance of the App Widget is removed from the host
          @Override
          public void onDeleted(Context context, int [] appWidgetId){
              super.onDeleted(context, appWidgetId);
             // Toast.makeText(context, "onDeleted()  ", Toast.LENGTH_SHORT).show();
          }
          // Called when last instance of App Widget is deleted from the App Widget host.
          @Override
          public void onDisabled(Context context) {
              super.onDisabled(context);
             // Toast.makeText(context, "onDisabled()  ", Toast.LENGTH_SHORT).show();
          }
  
      }
  </code></pre>