简体   繁体   English

Android Lollipop上的ConnectivityManager.EXTRA_NO_CONNECTIVITY始终为false

[英]ConnectivityManager.EXTRA_NO_CONNECTIVITY is always false on Android Lollipop

I am using this piece of code to detect Internet connection state changes. 我正在使用这段代码来检测Internet连接状态的变化。 It works fine on Android<5.0, but on API 21 this: 它适用于Android <5.0,但在API 21上:

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

is always false. 总是假的。 How to make this code to work on Android 5.0? 如何使此代码在Android 5.0上运行?

My BroadcastReceiver: 我的BroadcastReceiver:

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");
        }
    }
}

You can use NetworkRequest added in API level 21. 您可以使用API​​级别21中添加的NetworkRequest

Create a custom intent action: 创建自定义意图操作:

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

Create the new method registerConnectivityActionLollipop : 创建新方法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);
        }
    });
}

Add the new intent action to the intent filter and call registerConnectivityActionLollipop : 将新意图操作添加到intent过滤器并调用registerConnectivityActionLollipop

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

registerReceiver(mBroadcastReceiver, intentFilter);
registerConnectivityActionLollipop();

Change the BroadcastReceiver to support the new intent action: 更改BroadcastReceiver以支持新的intent操作:

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");
            }
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getBoolean(EXTRA_NO_CONNECTIVITY)始终返回false - getBoolean(EXTRA_NO_CONNECTIVITY) always returns false onReceive()android.net.ConnectivityManager.CONNECTIVITY_ACTION - onReceive() android.net.ConnectivityManager.CONNECTIVITY_ACTION connectivitymanager - android.net.conn.CONNECTIVITY_CHANGE - connectivitymanager - android.net.conn.CONNECTIVITY_CHANGE Android:ConnectivityManager始终返回null - Android: ConnectivityManager always returning null android google登录总是在棒棒糖或者上面运行错误handleSignInResult:false - android google sign in always error running in lollipop or above handleSignInResult:false ConnectivityManager.CONNECTIVITY_ACTION,注册接收器时总是广播? - ConnectivityManager.CONNECTIVITY_ACTION, always broadcast when registering a receiver? android ble中的方法writeDescriptor总是在棒棒糖中返回false - method writeDescriptor in android ble always return false in lollipop Android - ConnectivityManager.EXTRA_NETWORK_INFO已弃用 - Android - ConnectivityManager.EXTRA_NETWORK_INFO deprecated 在iOS中需要与Android中的“ ConnectivityManager.CONNECTIVITY_ACTION”相同的回调 - Need same callback in ios as “ConnectivityManager.CONNECTIVITY_ACTION” in android android 5.0(lollipop)中的蓝牙连接问题 - Bluetooth Connectivity issue in android 5.0(lollipop)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM