通过 ADB 获取蜂窝网络状态 shell

getting cellular network status via ADB shell

是否可以通过 ADB 检查 phone 是否连接到蜂窝网络?

我看过 ConnectivityManager,但它用于 android 应用程序开发。

当然使用来自 shell 的命令:

dumpsys telephony.registry

或直接来自 adb :

adb shell dumpsys telephony.registry

mServiceStatemDataConnectionState 的值会对您有所帮助。 我尝试在平面模式下 mServiceState=3 / mDataConnectionState=0 并连接到蜂窝网络:mServiceState=0 / mDataConnectionState=2

PS:我使用的是 Android 4.4 phone.

要添加到 Siri 的回答中,这里是 mServiceState 的值:

http://developer.android.com/reference/android/telephony/ServiceState.html

public static final int STATE_EMERGENCY_ONLY

Added in API level 1 The phone is registered and locked. Only emergency numbers are allowed.

Constant Value: 2 (0x00000002)

public static final int STATE_IN_SERVICE

Added in API level 1 Normal operation condition, the phone is registered with an operator either in home network or in roaming.

Constant Value: 0 (0x00000000)

public static final int STATE_OUT_OF_SERVICE

Added in API level 1 Phone is not registered with any operator, the phone can be currently searching a new operator to register to, or not searching to registration at all, or registration is denied, or radio signal is not available.

Constant Value: 1 (0x00000001)

public static final int STATE_POWER_OFF

Added in API level 1 Radio of telephony is explicitly powered off.

Constant Value: 3 (0x00000003)

这里是 mDataConnectionState 的值:

http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-0/android/telephony/TelephonyManager.java#TelephonyManager.0DATA_UNKNOWN

Data connection state: Unknown. Used before we know the state.

public static final int DATA_UNKNOWN        = -1;

Data connection state: Disconnected. IP traffic not available.

public static final int DATA_DISCONNECTED   = 0;

Data connection state: Currently setting up a data connection.

public static final int DATA_CONNECTING     = 1;

Data connection state: Connected. IP traffic should be available.

public static final int DATA_CONNECTED      = 2;

Data connection state: Suspended. The connection is up, but IP traffic is temporarily unavailable. For example, in a 2G network, data activity may be suspended when a voice call arrives.

public static final int DATA_SUSPENDED      = 3;

添加这个 post 有点晚了。我来到这个 post 寻找相同的结果,过了一会儿我也需要知道 wifi 状态。并进行更多调查...

# dumpsys wifi | grep curState                                
curState=ApStaDisabledState
curState=NotConnectedState
curState=InitialState
curState=UninitializedState
curState=InitialState

这将为您提供实际状态,无论是连接到 AccessPoint 还是仍在尝试连接,或者它是否已禁用或...

注意:如果您计划将结果用于时间紧迫的服务,您可能会寻找其他方式,因为有时等待响应可能需要一段时间(>5 秒)。