How to update widget once broadcast intent CONNECT

2019-03-31 12:20发布

问题:

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:

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.