ConnectivityManager.EXTRA_NO_CONNECTIVITY 在 Android Lollipop 上始终为 false

ConnectivityManager.EXTRA_NO_CONNECTIVITY is always false on Android Lollipop

我正在使用这段代码来检测 Internet 连接状态的变化。它在 Android<5.0 上运行良好,但在 API 21 上运行:

intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY)

总是错误的。如何使此代码在 Android 5.0 上运行?

我的广播接收器:

public class NetworkStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        if(intent.getExtras()!=null) {
            final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if(networkInfo != null && networkInfo.isConnectedOrConnecting()) {
                Log.d("receiver test", "detected on");
            }
        }
        Log.d("receiver test", Boolean.toString(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY)));
        if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
            Log.d("receiver test", "detected off");
        }
    }
}

您可以使用 API 级别 21 中添加的 NetworkRequest

创建自定义意图操作:

public static final String CONNECTIVITY_ACTION_LOLLIPOP = "com.example.CONNECTIVITY_ACTION_LOLLIPOP";

创建新方法registerConnectivityActionLollipop:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void registerConnectivityActionLollipop() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        return;

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder builder = new NetworkRequest.Builder();

    connectivityManager.registerNetworkCallback(builder.build(), new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            Intent intent = new Intent(CONNECTIVITY_ACTION_LOLLIPOP);
            intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

            sendBroadcast(intent);
        }

        @Override
        public void onLost(Network network) {
            Intent intent = new Intent(CONNECTIVITY_ACTION_LOLLIPOP);
            intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);

            sendBroadcast(intent);
        }
    });
}

将新的意图操作添加到意图过滤器并调用 registerConnectivityActionLollipop:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addAction(CONNECTIVITY_ACTION_LOLLIPOP);

registerReceiver(mBroadcastReceiver, intentFilter);
registerConnectivityActionLollipop();

更改 BroadcastReceiver 以支持新的意图操作:

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && TextUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION) ||
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && TextUtils.equals(intent.getAction(), CONNECTIVITY_ACTION_LOLLIPOP)) {

            if (intent.getExtras() != null) {
                final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
                    Log.d("receiver test", "detected on");
                }
            }

            Log.d("receiver test", Boolean.toString(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY)));
            if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
                Log.d("receiver test", "detected off");
            }
        }
    }
};