无论如何在Android的本机代码中获取移动设备的数据连接状态?我知道我们可以用Java中的ConnectivityManager来完成它。
Android的原生代码中有类似的东西吗?当他们从Gingerbread开始支持本地活动时,他们也会为连接管理员提供支持。
如果有人知道,请告诉我。我尝试了但是我没有在本地有类似的东西。我现在唯一的想法是在JAVA中使用连接管理器实现一个类,并从本机调用这些函数。
谢谢&问候,
SSuman185
答案 0 :(得分:0)
检查一下:
设置你的听众:
private static class MyPhoneStateListener extends PhoneStateListener{
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength){ /* Do Stuff */
}
catch(Exception ex)
{
}
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing " + incomingNumber;
break;
}
}
@Override
public void onCellLocationChanged(CellLocation location) {
try
{
super.onCellLocationChanged(location);
}
catch(Exception ex)
{ }
}
};
然后你必须注册听众:
public static void Init_DataRelatedStuff(Context ForeignContext)
{
foreignContext = ForeignContext;
m_psl = new MyPhoneStateListener();
m_telephonyManager = (TelephonyManager)foreignContext.getSystemService(Context.TELEPHONY_SERVICE);
m_telephonyManager.listen(m_psl, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
m_telephonyManager.listen(m_psl, PhoneStateListener.LISTEN_CELL_LOCATION);
m_telephonyManager.listen(m_psl, PhoneStateListener.LISTEN_CALL_STATE);
}
然后现在你可以获得数据状态:
public static String getDataState()
{
int nType = m_telephonyManager.getDataState();
String value = "UNKNOWN";
switch(nType)
{
case 0://DATA_DISCONNECTED
value = "DISCONNECTED";
break;
case 1://DATA_CONNECTING
value = "CONNECTING";
break;
case 2://DATA_CONNECTED
value = "CONNECTED";
break;
case 3://DATA_SUSPENDED
value = "SUSPENDED";
break;
default:
value = "UNKNOWN";
break;
}
return value;
}
你还需要在清单中获得许可:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />