开发者

getBoolean(EXTRA_NO_CONNECTIVITY) always returns false

开发者 https://www.devze.com 2023-04-01 13:54 出处:网络
One time that I had a prob, you guys were really helpful. So here I am again with another problem I have fallen onto... :/

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 not开发者_如何转开发e 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 the BroadcastReceiver for a purpose similar to yours.

My receiver does only catch the connectivity changes, but it does NOT evaluate the connectivity status. It informs the Service instead 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):

public class ConnectivityReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final Intent intentService = new Intent(context, MyService.class);
        intentService.putExtra(MyService.INTENT_HANDLE_CONNECTIVITY_CHANGE, "");

        context.startService(intentService);
    }
}

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):

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        final Bundle bundle = intent.getExtras();

        // Handle the intent INTENT_HANDLE_CONNECTIVITY_CHANGEif any
        if ((bundle != null) && (bundle.get(INTENT_HANDLE_CONNECTIVITY_CHANGE) != null)) {         
            handleConnectivity(); 
        }
    }

    return START_STICKY;
}

private void handleConnectivity() {
    final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if(netInfo != null && netInfo.isConnected()) {
        // WE ARE CONNECTED: DO SOMETHING
    }   
    else {
        // WE ARE NOT: DO SOMETHING ELSE    
    }
}

This works fine, under Android-7 (2.1) as well.


Don't know about the strange NO_CONNECTIVITY flag, but you can retrieve the active network from the intent:

(NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

You can probably just call isConnected() on this object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号