For the many threads, blogs, examples and tutorials on the topic of
Broadcast Receivers and mobile data connectivity I have not seen this
question asked or answered.
I believe, based on experimenting with one of my Apps, that the answer to
this question is a distinct NO, that while WiFi is enabled, a Broadcast
Receiver listening for Mobile Data CONNECTIVITY_CHANGE does not receive a
broadcast notification when that event occurs. If I am wrong and have
missed something or if you can confirm this independently please let me
know.
My App is a home screen Widget with two classes, ActiveMobileData is the
AppWidgetProvider and ConnectivityChangeReceiver is the BroadcastReceiver.
There is no App just the home screen widget. It simply toggles a home
screen icon between red and green to indicate the current mobile data
state. It has worked perfectly for several months with about 100 users.
I decided to add the BroadcastReceiver to pick up clicks from Settings.
This code is also straight forward - it determines the current state of
mobile data, and uses a global boolean variable set by AppWidgetProvider to
determine if the home screen icon is red or green. Then it simply ensures
that the icon color matches the mobile data state.
It all works except that when WiFi is enabled the BroadcastReceiver does
not get a notification when the user turns on mobile data from Settings. If
there is a way around this limitation i would appreciate hearing about it.
I do think that the code will not help to answer the question as my
assertion is either true or false regardless and i hope that someone has
knowledge of this, but here is the code for the widget and then for the
receiver. I left out some details to keep it somewhat brief.* iconEnabled*
is the global boolean variable shared between the AppWidgetProvider and
BroadcastReceiver classes ...
*public class ActiveMobileData extends AppWidgetProvider {*
*static boolean iconEnabled;*
*@Override*
*public void onReceive(Context context, Intent intent) {*
* if (intent.getAction() != null)*
* super.onReceive(context, intent);*
* else {*
* context.startService(new Intent(context, ToggleService.class));*
* }*
*}*
*@Override*
*public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[]appWidgetIds) {*
* context.startService(new Intent(context, ToggleService.class));*
*}*
*public static class ToggleService extends IntentService {*
* public ToggleService() {*
* super("ActiveMobileData$ToggleService");*
* }*
* @Override*
* protected void onHandleIntent(Intent intent) {*
* ComponentName cn = new ComponentName(this,
ActiveMobileData.class);*
* AppWidgetManager mgr = AppWidgetManager.getInstance(this);*
* mgr.updateAppWidget(cn, buildUpdate(this));*
* }*
* private RemoteViews buildUpdate(Context context) {*
* RemoteViews updateViews = new
RemoteViews(context.getPackageName(), R.layout.widget);*
* if (!isMobileDataEnabled(getApplicationContext())) {*
* updateViews.setImageViewResource(R.id.mobileDataState,
R.mipmap.ic_launcher_g);*
* enableMobileData(getApplicationContext(), true);*
* iconEnabled = true;*
* } else {*
* updateViews.setImageViewResource(R.id.mobileDataState,
R.mipmap.ic_launcher_r);*
* enableMobileData(getApplicationContext(), false);*
* iconEnabled = false;*
* }*
* Intent i = new Intent(this, ActiveMobileData.class);*
* PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);*
* updateViews.setOnClickPendingIntent(R.id.mobileDataState, pi);*
* return updateViews;*
* }*
* public boolean isMobileDataEnabled(Context context) {*
* // ... the code here is the one that uses Java reflection*
* }*
* private void enableMobileData(Context context, boolean enabled) {*
* // ... the code here is the one that uses Java reflection*
* }*
* } // public static class ToggleService*
*} // public class ActiveMobileData*
Following is the code for the BroadcastReceiver ...
*public class ConnectivityChangeReceiver extends BroadcastReceiver {*
* @Override*
* public void onReceive (Context context, Intent intent) {*
* handleIntent(context);*
* }*
* protected void handleIntent(Context context) {*
* ComponentName cn = new ComponentName(context,
ActiveMobileData.class);*
* AppWidgetManager mgr = AppWidgetManager.getInstance(context);*
* mgr.updateAppWidget(cn, buildUpdate(context));*
* }*
* private RemoteViews buildUpdate(Context context) {*
* RemoteViews updateViews = new
RemoteViews(context.getPackageName(), R.layout.widget);*
* if (!ActiveMobileData.iconEnabled && isMobileDataEnabled(context))
{*
* ActiveMobileData.iconEnabled = true;*
* updateViews.setImageViewResource(R.id.mobileDataState,
R.mipmap.ic_launcher_g);*
* Intent i = new Intent(context, ActiveMobileData.class);*
* PendingIntent pi = PendingIntent.getBroadcast(context, 0, i,
0);*
* updateViews.setOnClickPendingIntent(R.id.mobileDataState, pi);*
* } else*
* if (ActiveMobileData.iconEnabled && !isMobileDataEnabled(context))
{*
* ActiveMobileData.iconEnabled = false;*
* updateViews.setImageViewResource(R.id.mobileDataState,
R.mipmap.ic_launcher_r);*
* Intent i = new Intent(context, ActiveMobileData.class);*
* PendingIntent pi = PendingIntent.getBroadcast(context, 0, i,
0);*
* updateViews.setOnClickPendingIntent(R.id.mobileDataState, pi);*
* }*
* return updateViews;*
* }*
* private boolean isMobileDataEnabled(Context context) {*
* // ... Identical code to that in the AppWidgetProvider*
* }*
*} // class ConnectivityChangeReceiver*
*Mick*
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/94e80d48-bedf-4ba0-b82f-b29d687981da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.