由于业务场景需要用到根据网络连接自动化执行操作,但android开机后连接广播又感觉不靠谱,因此就这个广播研究了一下参数,大致流程如下:

有sim卡和wifi联网广播顺序

大致代码如下:

switch (intent.getAction()) {
        case ConnectivityManager.CONNECTIVITY_ACTION:  //开机会有一次广播
            NetworkInfo networkInfo = intent.getParcelableExtra("networkInfo");
            if (NetUtils.isNetConnected(context) && networkInfo.isConnected()) {
                Bundle extras = intent.getExtras();
                Set<String> strings = extras.keySet();
                for (String keyStr:strings) {
                    if(extras.get(keyStr) instanceof Integer){
                        Log.v(TAG,"intent extras(int) :"+ keyStr + ":" + extras.get(keyStr));
                    }else if(extras.get(keyStr) instanceof String){
                        Log.v(TAG,"intent extras(String) :" + keyStr + ":" + extras.get(keyStr));
                    }else{
                        Log.v(TAG,"intent extras() :" + keyStr + ":" + extras.get(keyStr));
                    }
                }
                Log.i(TAG, "网络连接成功 action=" + intent.getAction() + ";mCurrDeviceStatus=" + mCurrDeviceStatus);
                }
            } else {
                Log.e(TAG, "网络断开");
            }
            break;
    }

看下广播日志:

2019-10-21 18:31:16.119 1592-1592/xxx.xxx I/SplashFragmentNew: onCallStateChanged state=0;incomingNumber=;simCard=SIM_CARD_1
2019-10-21 18:31:16.119 1592-1592/xxx.xxx I/SplashFragmentNew: onCallStateChanged state=0;incomingNumber=;simCard=SIM_CARD_1
2019-10-21 18:31:16.618 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras() :networkInfo:[type: MOBILE[EDGE], state: CONNECTED/CONNECTED, reason: (unspecified), extra: cmnet, failover: false, available: true, roaming: false]
2019-10-21 18:31:16.618 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(int) :networkType:0
2019-10-21 18:31:16.619 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(int) :inetCondition:0
2019-10-21 18:31:16.619 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(String) :extraInfo:cmnet
2019-10-21 18:31:16.619 1592-1592/xxx.xxx I/SplashFragmentNew: 网络连接成功 action=android.net.conn.CONNECTIVITY_CHANGE;mCurrDeviceStatus=INIT

2019-10-21 18:31:17.910 1592-1592/xxx.xxx I/SplashFragmentNew: onCallStateChanged state=0;incomingNumber=;simCard=SIM_CARD_1
2019-10-21 18:31:17.911 1592-1592/xxx.xxx I/SplashFragmentNew: onCallStateChanged state=0;incomingNumber=;simCard=SIM_CARD_1
2019-10-21 18:31:20.060 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras() :networkInfo:[type: MOBILE[EDGE], state: DISCONNECTED/DISCONNECTED, reason: connected, extra: cmnet, failover: false, available: true, roaming: false]
2019-10-21 18:31:20.060 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(int) :networkType:0
2019-10-21 18:31:20.064 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(String) :reason:connected
2019-10-21 18:31:20.064 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(int) :inetCondition:0
2019-10-21 18:31:20.064 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(String) :extraInfo:cmnet
2019-10-21 18:31:20.064 1592-1592/xxx.xxx I/SplashFragmentNew: 网络连接成功 action=android.net.conn.CONNECTIVITY_CHANGE;mCurrDeviceStatus=INIT

2019-10-21 18:31:20.080 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras() :networkInfo:[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: true, roaming: false]
2019-10-21 18:31:20.081 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(int) :networkType:1
2019-10-21 18:31:20.081 1592-1592/xxx.xxx V/SplashFragmentNew: intent extras(int) :inetCondition:0
2019-10-21 18:31:20.081 1592-1592/xxx.xxx I/SplashFragmentNew: 网络连接成功 action=android.net.conn.CONNECTIVITY_CHANGE;mCurrDeviceStatus=INIT
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐