【问题标题】:ConnectivityManager.EXTRA_NO_CONNECTIVITY is always false on Android LollipopConnectivityManager.EXTRA_NO_CONNECTIVITY 在 Android Lollipop 上始终为 false
【发布时间】:2015-04-16 14:31:19
【问题描述】:

我正在使用这段代码来检测 Internet 连接状态的变化。它在 Android

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

【问题讨论】:

    标签: android broadcastreceiver android-5.0-lollipop android-internet


    【解决方案1】:

    您可以使用 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");
                }
            }
        }
    };
    

    【讨论】:

    • 谢谢。我不得不稍微修改您的解决方案以满足我的需求,但它让我对如何解决 Android 5.0 的问题有了一个总体了解
    • @MattiaMaestrini:你的代码不是倒过来了吗?不应该是intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 中的onAvailable 吗?
    • @Jim 你是对的。感谢您指出这一点,我刚刚修复了答案。
    • 所以基本上我们的想法是我们重新提出意图,以便在以前的 SDK 中运行良好的代码可以在 21+ 中运行,对吗?但是为什么不直接在registerConnectivityActionLollipop 中处理呢?我们已经检查了我们正在运行的 SDK
    • 我正在尝试您的代码。 onAvailableonLost 被调用,但是当我检查 intent.getAction() 它总是 android.net.conn.CONNECTIVITY_CHANGE 而不是 com.example.CONNECTIVITY_ACTION_LOLLIPOP
    猜你喜欢
    • 2021-02-16
    • 2021-03-19
    • 2022-06-28
    • 2012-03-29
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    相关资源
    最近更新 更多