在android的本机代码中,是否仍然可以获取手机的数据连接状态?
在Android的本机代码中是否有获取手机数据连接状态的方法?我知道我们可以用Java中的ConnectionManager来实现这一点 Android的本地代码中有类似的东西吗?当他们从姜饼开始支持本地活动时,他们也为connectivity manager提供了支持 如果有人知道,请告诉我。我试过了,但在native中没有类似的东西。到目前为止,我唯一的想法是在JAVA中使用Connectivity Manager实现一个类,并从本机调用这些函数 谢谢和问候,在android的本机代码中,是否仍然可以获取手机的数据连接状态?,android,android-ndk,native,Android,Android Ndk,Native,在Android的本机代码中是否有获取手机数据连接状态的方法?我知道我们可以用Java中的ConnectionManager来实现这一点 Android的本地代码中有类似的东西吗?当他们从姜饼开始支持本地活动时,他们也为connectivity manager提供了支持 如果有人知道,请告诉我。我试过了,但在native中没有类似的东西。到目前为止,我唯一的想法是在JAVA中使用Connectivity Manager实现一个类,并从本机调用这些函数 谢谢和问候, SSuman185检查以下内容
SSuman185检查以下内容: 设置您的侦听器:
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);
}
})
然后您必须注册侦听器:
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" />