Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8207233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:55:54+00:00 2026-06-07T08:55:54+00:00

I want to update the widget text and its color whenever Network-packet-data connection changes.

  • 0

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.

I am noticing a very strange behavior whenever I enable/disable the connection onReceive() is getting triggered two times. In first instance I receive NetworkInfo 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 NetworkInfo object is null.

here is my manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.simplewidget"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <!-- Permissions -->
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <!-- Widget Broadcast receiver -->
        <receiver
            android:name=".ExampleAppWidgetProvider"
            android:label="Widget ErrorBuster" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget1_info" />
        </receiver>
        <service android:name=".UpdateWidgetService"></service>
    </application>
</manifest>

My AppWidgetProvider class is as following.

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 > 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;

 }
}

Service class which enables/disables the Network-packet-data connection.

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;

    }
}

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.

Is there any other way I can update the widget text/color once I receive CONNECTIVITY_CHANGE intent?

Edit:

I couldn’t figured out why I am getting two CONNECTIVITY_CHANGE intent in AppWidgetProvider, but I got to learn How to update the widget from Broadcast receiver and below is the 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);

        }
    }

}

If someone knows why I am getting two CONNECTIVITY_CHANGE intent, please share your thoughts here. you help would be really appreciated here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:55:56+00:00Added an answer on June 7, 2026 at 8:55 am

    Now I have discovered why onReceive() gets called twice when button is clicked to enable/disable the network.
    1. First time it gets called when APPWIDGET_UPDATE intent broadcasts.
    2. Second time it gets called when CONNECTIVITY_CHANGE intent broadcasts.

    Anyway I am able to update the widget from Broadcast receiver, the source code has been posted in the above post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have ios application with xcdatamodeld, i want update some data so need a
I want to update my PHP GUI view when my data source is updated(data
I want to update the lastlogin column value .i wrote the code like this
I have a 4x4 widget and i want to update a little piece of
My problem is similar to this one, How to update a widget every minute
I'm writing an Android widget. I want to update it every 1-5 minutes to
I want to escape the inputs to this form so that when its enter
I want to change text color inside a rectangle periodically. Here is my trial:
I want to fill a text input in my form using an autocomplete widget
i want update textarea content onclock on a input button for example: <input type=button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.