title: Start/Stop Service from Widget
body: <p>I want to start a service from inside the widget, I know that i can do it using PendingIntent like</p>
  
  <pre><code>PendingIntent intent = PendingIntent.getService(context, 0, new Intent(context, MyService.class), Intent.FLAG_ACTIVITY_NEW_TASK);
  remoteViews.setOnClickPendingIntent(R.id.buttonWidgetStartService, intent);
  </code></pre>
  
  <p>but the problem is that I've already used PendingIntent on that button for some other purpose.
  So, I tried starting the service using</p>
  
  <pre><code>context.startService(new Intent(context, MyService.class));
  </code></pre>
  
  <p>and here I'm getting <code>NullPointerException</code>.</p>
  
  <p><strong>Here's my code:</strong></p>
  
  <pre><code>public class WiNetWidget extends AppWidgetProvider {
  
      public static String TOGGLE_WINET = "ToggleWiNetService";
      private static boolean serviceRunning = false;
      private static Intent serviceIntent;
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          final int N = appWidgetIds.length;
          serviceIntent = new Intent(context, WiNetService.class);
  
          for (int i=0; i&lt;N; i++) {
              int appWidgetId = appWidgetIds[i];
              RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_winet);
  
              remoteViews.setViewVisibility(R.id.buttonWidgetLoading, View.INVISIBLE);
              remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.VISIBLE);
  
              Intent newIntent = new Intent(context, WiNetWidget.class);
              newIntent.setAction(TOGGLE_WINET);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
              remoteViews.setOnClickPendingIntent(R.id.buttonWidgetStartService, pendingIntent);
              remoteViews.setOnClickPendingIntent(R.id.buttonWidgetStopService, pendingIntent);
  
              appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
              Log.i(TOGGLE_WINET, "updated");
          }
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if(intent.getAction().equals(TOGGLE_WINET)) {
              RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_winet);
              if(serviceRunning) {
                  context.stopService(serviceIntent);
                  remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.VISIBLE);
                  remoteViews.setViewVisibility(R.id.buttonWidgetStopService, View.INVISIBLE);
                  Toast.makeText(context, "serviceStopped", Toast.LENGTH_SHORT).show();
              } else {
                  context.startService(serviceIntent);
                  remoteViews.setViewVisibility(R.id.buttonWidgetStopService, View.VISIBLE);
                  remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.INVISIBLE);
                  Toast.makeText(context, "serviceStarted", Toast.LENGTH_SHORT).show();
              }
              serviceRunning=!serviceRunning;
              ComponentName componentName = new ComponentName(context, WiNetWidget.class);
              AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);
          }
          super.onReceive(context, intent);
      }
  }
  </code></pre>
  
  <p>Note: If I remove the lines containing <code>startService</code> and <code>stopService</code>, everything is running smoothly.</p>
  
  <p>Thanks in Advance!! :)</p>