ConnectivityManager.EXTRA_NO_CONNECTIVITY is always false on Android Lollipop

前端 未结 1 2071
甜味超标
甜味超标 2021-02-08 10:42

I am using this piece of code to detect Internet connection state changes. It works fine on Android<5.0, but on API 21 this:

intent.getExtras().getBoolean(Con         


        
1条回答
  •  后悔当初
    2021-02-08 11:31

    You can use NetworkRequest added in API level 21.

    Create a custom intent action:

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

    Create the new method 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:

    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:

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

    0 讨论(0)
提交回复
热议问题