One time that I had a prob, you guys were really helpful. So here I am again with another problem I have fallen onto… :/
I am running a custom NetworkReceiver that extends BroadcastReceiver. I want to detect when the phone has an internet connection so that I can start a service whenever it does. I have read a lot of topics and all topics more or less prompt to the following code which DOES NOT WORK for me:
public class NetworkReceiver extends BroadcastReceiver {
private static final String tag = NetworkReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(tag,"****** NetworkReceiver // onReceive()");
boolean noConnection = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.i(tag,"****** NetworkReceiver // Network Down?: " + intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false));
}
// TODO Bug: Never stops the service. Stays always connected!
if(noConnection) {
Log.i(tag,"****** NetworkReceiver // Stopping Service...");
context.stopService(new Intent(context, FetchService.class));
} else {
Log.i(tag,"****** NetworkReceiver // Starting Service!");
context.startService(new Intent(context, FetchService.class));
}
}
The problem I have is that the logcat always returns NetworkDown?: false and the service never stops (it restarts all the time). I tried it on the emulator by pressing F8 and I also tried it on my dev-phone (it has no SIM, internet access is by my wifi router) by switching off the router -> no internet access guaranteed!
My Manifest includes the following lines:
...
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".NetworkReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
...
I should also note that I also tried
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" /> in my manifest file with no change and that when I altered the line:
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)
to
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true)
the result was always true – and thus it always tried to stop my service and never start it.
I suspect that the Extras from the Intent are not read at all and that is why the default value of getBooleanExtra() always returns.
PS. I am on Android 2.1
Thanks!
I am not sure for the Extras that cannot be read inside the
BroadcastReceiver, but I can show you athe way I use theBroadcastReceiverfor a purpose similar to yours.My receiver does only catch the connectivity changes, but it does NOT evaluate the connectivity status. It informs the
Serviceinstead as in both cases (connected or not) this service will have something to do. This service can be running or not at that time, so it will either be started or “informed” (remember that you can safely call startService() several times, these calls do not nest):Then, in my service, I do evaluate the connectivity and then act accordingly. Notice that I don’t evaluate the connection status the same way as you do, and this might be why the code below works but not yours (below is the relevant code only):
This works fine, under Android-7 (2.1) as well.