title: Android-Widget : Calling BroadcastReceiver onReceive from onUpdate of Appwidgetprovider
body: <p>I am making a flashlight widget which will toggle the flashlight on/off and also I am trying to toggle the icon of the widget-button on clicking the widget button, for this I have the  Appwidgetprovider whose onUpdate will use RemoteViews and call the BroadcastReceiver.</p>
  
  <p>In the BroadcastReceiver, the onReceive function will perform the flashlight toggle and the icon toggle for the widget.
  The issue I am facing is that the onReceive function is not being called and no action happening with the widget.</p>
  
  <p>below is the code:</p>
  
  <p>AppWidgetProvider class:</p>
  
  <pre><code>public class WidgetActivity extends AppWidgetProvider {
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          Intent receiver = new Intent(context, WidgetActivity.class);
          receiver.setAction("COM_FLASHLIGHT");
          receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);
  
          RemoteViews views = new RemoteViews(context.getPackageName(),
                  R.layout.widget_flash_layout);
          views.setOnClickPendingIntent(R.id.imageButton1, pendingIntent);
  
          appWidgetManager.updateAppWidget(appWidgetIds, views);
  
      }
  }
  </code></pre>
  
  <p>BroadcastReceiver Class:</p>
  
  <pre><code>public class WidgetService extends BroadcastReceiver {
      private static boolean isLightOn = false;
      private static Camera camera;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
  
          RemoteViews views = new RemoteViews(context.getPackageName(),
                  R.layout.widget_flash_layout);
  
          if (isLightOn) {
              views.setImageViewResource(R.id.imageButton1,
                      R.drawable.light_off_widget);
          } else {
              views.setImageViewResource(R.id.imageButton1,
                      R.drawable.light_on_widget);
          }
  
          AppWidgetManager appWidgetManager = AppWidgetManager
                  .getInstance(context);
          appWidgetManager.updateAppWidget(new ComponentName(context,
                  WidgetActivity.class), views);
  
          if (isLightOn) {
              if (camera != null) {
                  camera.stopPreview();
                  camera.release();
                  camera = null;
                  isLightOn = false;
              }
  
          } else {
              // Open the default i.e. the first rear facing camera.
              camera = Camera.open();
  
              if (camera == null) {
                  Toast.makeText(context, "no camera", Toast.LENGTH_SHORT).show();
              } else {
                  // Set the torch flash mode
                  Parameters param = camera.getParameters();
                  param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                  try {
                      camera.setParameters(param);
                      camera.startPreview();
                      isLightOn = true;
                  } catch (Exception e) {
                      Toast.makeText(context, "no flash", Toast.LENGTH_SHORT)
                              .show();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Manifest:</p>
  
  <pre><code>&lt;receiver android:name="com.widget.WidgetActivity"&gt;
      &lt;intent-filter&gt;
                &lt;action
                   android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt;
      &lt;/intent-filter&gt;
      &lt;meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/flash_widget" /&gt;
  &lt;/receiver&gt;
  
  &lt;receiver android:name="com.widget.WidgetService"&gt;
  
      &lt;action android:name="COM_FLASHLIGHT"&gt;&lt;/action&gt;
  
  &lt;/receiver&gt;
  </code></pre>
  
  <p>In the manifest I have <strong>not</strong> wrapped the <code>&lt;action&gt;</code> tag of the widget service with 
   <code>&lt;intent-filter&gt;</code> as it was showing a warning saying "Exported receiver does not require permission".</p>