BroadcastReceiver for CONNECTIVITY_ACTION always returns null in intent.getExtras()

≯℡__Kan透↙ 提交于 2019-12-18 11:32:04

问题



Im trying to receive BroadcastMessages from CONNECTIVITY_ACTION:

    // register BroadcastReceiver on network state changes
    final IntentFilter mIFNetwork = new IntentFilter();
    mIFNetwork.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
    registerReceiver(mIRNetwork, mIFNetwork);

and receiver is:

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

        android.util.Log.i(TAG,"mIRNetwork: Network State Received: "+intent.getAction());
        Bundle extras = intent.getExtras();
        if (extras!=null){
                android.util.Log.i(TAG,"mIRNetwork: ACTION_BACKGROUND_DATA_SETTING_CHANGED: "+extras.getString(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
                android.util.Log.i(TAG,"mIRNetwork: CONNECTIVITY_ACTION: "+extras.getString(ConnectivityManager.CONNECTIVITY_ACTION));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_EXTRA_INFO: "+extras.getString(ConnectivityManager.EXTRA_EXTRA_INFO));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_NO_CONNECTIVITY: "+extras.getString(ConnectivityManager.EXTRA_NO_CONNECTIVITY));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_REASON: "+extras.getString(ConnectivityManager.EXTRA_REASON));

            }
        }

in short extras is always null. I thought that if i loose WiFi connection i should get EXTRA_NO_CONNECTIVITY (cuz its the only way to Internet) or at least something from the list. But no luck. If i disconnect my WiFi AP receiver gets his message but with null extras. When i turn my WiFi back on once again receiver fires but no extras... Why is that? How to know that app lost any network connection? I thought its the way.


回答1:


You can not get extra but you can get data by this way

private class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager
                .EXTRA_NO_CONNECTIVITY, false);
        NetworkInfo info1 = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager
                .EXTRA_NETWORK_INFO);
        NetworkInfo info2 = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager
                .EXTRA_OTHER_NETWORK_INFO);
        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
        boolean failOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
        Log.d("MY_TAG", "onReceive(): mNetworkInfo=" + info1 + " mOtherNetworkInfo = " +
                (info2 == null ? "[none]" : info2 + " noConn=" + noConnectivity));
    }
}

For more info see this

http://code.google.com/p/androidwisprclient/source/browse/trunk/src/com/joan/pruebas/NetworkConnectivityListener.java?r=2




回答2:


Dharmendras answer is good. However, note that EXTRA_NETWORK_INFO is now deprecated (since Api Level 14) and the Android docs say the following:

Since NetworkInfo can vary based on UID, applications should always obtain network information through getActiveNetworkInfo().

That actually makes things very easy for us. You could reuse the connectivity check you probably did before and do something like this:

private class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        unregisterReceiver(this)
        checkConnection();
    }
}

private void checkConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo()
            .isConnectedOrConnecting()) {
        // do something
    }
}

Assuming you're inside the activity that registered the broadcast of course.

This also has the added benefit of following the best practices for only listening for the connectivity broadcast as briefly as possible, outlined here :)



来源:https://stackoverflow.com/questions/6924006/broadcastreceiver-for-connectivity-action-always-returns-null-in-intent-getextra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!