title: Android: Run method in separate class from a home screen widget
body: <p>I have a method in a plain class file (not an activity) that I run from my main activity, and I also want to run it from my home screen widget. </p>
  
  <p>The button in the widget works perfectly as long as the app is open in the background. If I close the app from recent tasks the button stops working. </p>
  
  <p>I think I need to start the app with the button click as well, but I don't want it to open the main activity, just run the method. How can I do this?</p>
  
  <p>Here is my code for the widget:</p>
  
  <pre><code>public class PanicWidget extends AppWidgetProvider {
  
      public static final String CLICK_PANIC = "PANIC";
  
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.panic_widget);
          Intent intent = new Intent(context, PanicWidget.class);
          intent.setAction(CLICK_PANIC);
  
          PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  
          remoteViews.setOnClickPendingIntent(R.id.panicbutton, actionPendingIntent);
  
          appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
      }
  
      @Override
      public void onReceive(Context context, Intent intent){
          super.onReceive(context, intent);
  
          if (CLICK_PANIC.equals(intent.getAction())){
              Methods.countdown();
          }
      }
  }
  </code></pre>