title: How to update widget once broadcast intent CONNECTIVITY_CHANGE is received
body: <p>I want to update the widget text and its color whenever Network-packet-data connection changes. Even this widget can enable/disable Network-packet-data. 
  Broadcast receiver for android.net.conn.CONNECTIVITY_CHANGE has been register in Android manifest file and onReceive() is already  overridden in AppWidgetProvider class.</p>
  
  <p>I am noticing a very strange behavior whenever I enable/disable the connection onReceive() is getting triggered two times. In first instance I receive <code>NetworkInfo</code> object but in second instance I don't receive the info instance. 
    But when I enable/disable connection by the system setting onReceive() is getting triggered once and <code>NetworkInfo</code> object is null.</p>
  
  <p>here is my manifest file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.rakesh.simplewidget"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk android:minSdkVersion="10" /&gt;
      &lt;!-- Permissions --&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
  
          &lt;!-- Widget Broadcast receiver --&gt;
          &lt;receiver
              android:name=".ExampleAppWidgetProvider"
              android:label="Widget ErrorBuster" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt;
                  &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
              &lt;/intent-filter&gt;
  
              &lt;meta-data
                  android:name="android.appwidget.provider"
                  android:resource="@xml/widget1_info" /&gt;
          &lt;/receiver&gt;
          &lt;service android:name=".UpdateWidgetService"&gt;&lt;/service&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>My AppWidgetProvider class is as following.</p>
  
  <pre><code>public class ExampleAppWidgetProvider extends AppWidgetProvider {
  
   int[] mAppWidgetIds;
  
   public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  
    final int N = appWidgetIds.length;
    this.mAppWidgetIds = appWidgetIds;
  
  
    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
     ExampleAppWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
  
    // Build the intent to call the service
    Intent intent = new Intent(context.getApplicationContext(),
     UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
  
    // Update the widgets via the service
    context.startService(intent);
   }
  
   @Override
   public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
  
    StringBuilder str = new StringBuilder();
    if (info != null) {
     str.append(" info is NULL");
    }
    if (checkConnectivityState(context)) {
     str.append("; data is enable");
    } else {
     str.append("; data is disable");
    }
    Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
   }
  
   public static void updateAppWidget(Context context,
    int[] appWidgetIds, boolean enable) {
    if (appWidgetIds == null) {
     return;
    }
    if (appWidgetIds.length &gt; 0) {
  
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
     RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
     if (enable) {
      updateViews.setTextColor(R.id.BtEnableDisable, Color.GREEN);
      updateViews.setTextViewText(R.id.BtEnableDisable, "Enabled");
  
     } else {
      updateViews.setTextColor(R.id.BtEnableDisable, Color.GRAY);
      updateViews.setTextViewText(R.id.BtEnableDisable, "Disabled");
     }
     appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
     Toast.makeText(context, "updateAppWidget() ", Toast.LENGTH_SHORT).show();
  
    }
   }
  
   private boolean checkConnectivityState(Context context) {
    final TelephonyManager telephonyManager = (TelephonyManager) context
     .getSystemService(Context.TELEPHONY_SERVICE);
    return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED;
  
   }
  }
  </code></pre>
  
  <p>Service class which enables/disables the Network-packet-data connection. </p>
  
  <pre><code>public class UpdateWidgetService extends Service {
      private static final String LOG = "de.vogella.android.widget.example";
  
      @Override
      public void onStart(Intent intent, int startId) {
          Log.i(LOG, "Called");
          // Create some random data
  
          AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
                  .getApplicationContext());
  
          int[] allWidgetIds = intent
                  .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
  
          ComponentName thisWidget = new ComponentName(getApplicationContext(),
                  ExampleAppWidgetProvider.class);
          int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
          Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
          Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
  
          for (int widgetId : allWidgetIds) {
              // Create some random data
              int number = (new Random().nextInt(100));
  
              RemoteViews remoteViews = new RemoteViews(this
                      .getApplicationContext().getPackageName(),
                      R.layout.widget1);
              Log.w("WidgetExample", String.valueOf(number));
              EnableDisableConnectivity edConn = new EnableDisableConnectivity(this.getApplicationContext());
              edConn.enableDisableDataPacketConnection(!checkConnectivityState(this.getApplicationContext()));
  
              // Register an onClickListener
              Intent clickIntent = new Intent(this.getApplicationContext(),
                      ExampleAppWidgetProvider.class);
  
              clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
              clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                      allWidgetIds);
  
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,
                      PendingIntent.FLAG_UPDATE_CURRENT);
              remoteViews.setOnClickPendingIntent(R.id.BtEnableDisable, pendingIntent);
              appWidgetManager.updateAppWidget(widgetId, remoteViews);
          }
          stopSelf();
  
          super.onStart(intent, startId);
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      private boolean checkConnectivityState(Context context){
          final TelephonyManager telephonyManager = (TelephonyManager) context
                  .getSystemService(Context.TELEPHONY_SERVICE);
          return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED;
  
      }
  }
  </code></pre>
  
  <p>I also tried with a Broadcast Receiver class there I receive the NetworkInfo whenever there is change in connection. But I don't know how to update the widget text/color from broadcast receiver.</p>
  
  <p>Is there any other way I can update the widget text/color once I receive CONNECTIVITY_CHANGE intent?</p>
  
  <h2><strong><em>Edit:</em></strong></h2>
  
  <p>I couldn't figured out why I am getting two CONNECTIVITY_CHANGE intent in <code>AppWidgetProvider</code>, but I got to learn How to update the widget from Broadcast receiver and below is the code.</p>
  
  <pre><code>public class ConnectivityReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          NetworkInfo info = (NetworkInfo)intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
  
          if(info.getType() == ConnectivityManager.TYPE_MOBILE){
  
              RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                      R.layout.widget1);
  
              if(info.isConnectedOrConnecting()){
                  Toast.makeText(context, "Data packet enabled", Toast.LENGTH_SHORT).show();
                  Log.d("RK","Mobile data is enabled");
                  remoteViews.setTextColor(R.id.BtEnableDisable, Color.GREEN);
                  remoteViews.setTextViewText(R.id.BtEnableDisable, "Enabled");
              }else{
                  Toast.makeText(context, "Data packet disabled", Toast.LENGTH_SHORT).show();
                  Log.e("RK","Mobile data is disconnected");
                  remoteViews.setTextColor(R.id.BtEnableDisable, Color.BLACK);
                  remoteViews.setTextViewText(R.id.BtEnableDisable,"Disabled");
              }
  
              ComponentName thiswidget = new ComponentName(context, ExampleAppWidgetProvider.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(context);
              manager.updateAppWidget(thiswidget, remoteViews);
  
          }
      }
  
  }
  </code></pre>
  
  <p>If someone knows why I am getting two CONNECTIVITY_CHANGE intent, please share your thoughts here. you help would be really appreciated here.</p>