我们通常检测手机卡的有无服务 通常都用的是TelephonyManager
然后注册
telMgr = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
telMgr.listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE //注册电话的状态
| PhoneStateListener.LISTEN_DATA_ACTIVITY //注册数据 的状态
| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
| PhoneStateListener.LISTEN_SERVICE_STATE //注册手机卡有无服务
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS //获取手机信号
| PhoneStateListener.LISTEN_PRECISE_DATA_CONNECTION_STATE
);
注测手机卡信号有无服务这里电信卡有一个坑, 就是当有服务的时候,电信卡在Listener 里面的返回数据还是无服务。被这个坑了,导致自己的状态栏在电信卡的时候显示无服务。
private class MyPhoneStateListener extends PhoneStateListener {
public void onSignalStrengthsChanged(SignalStrength signalStrength) { //这个方法可以获取收及信号的强度
super.onSignalStrengthsChanged(signalStrength);
....
}
@Override
public void onServiceStateChanged(ServiceState serviceState) { //本文主要是这个方法
super.onServiceStateChanged(serviceState);
Log.i(LOG_TAG, "onServiceStateChanged: serviceState" + serviceState);
switch (serviceState.getState()) {
case ServiceState.STATE_IN_SERVICE: //当手机卡已经注册服务的时候,会回调这个方法。
isNosim=false;
Log.i(LOG_TAG, "onServiceStateChanged: STATE_IN_SERVICE");
break;
case ServiceState.STATE_OUT_OF_SERVICE: //当手机卡无服务的时候会回调这个方法 ,但是电信卡的注册了服务,也是会回调STATE_OUT_OF_SERVICE 这个方法。
case ServiceState.STATE_EMERGENCY_ONLY:
isNosim = !(serviceState.getDataRegState() == ServiceState.STATE_IN_SERVICE); //chc modify cdma 4G
if(isNosim){
singleLevel = -1;
showRSSI(singleLevel,NETWORN_NONE);
}
Log.i(LOG_TAG, "onServiceStateChanged: STATE_OUT_OF_SERVICE isNosim="+isNosim);
break;
case ServiceState.STATE_POWER_OFF:
Log.i(LOG_TAG, "onServiceStateChanged: STATE_POWER_OFF");
break;
}
}
所以电信卡就多了一层判断,
当PhoneStateListener 回调
ServiceState.STATE_OUT_OF_SERVICE:
ServiceState.STATE_EMERGENCY_ONLY:
serviceState.getDataRegState() == ServiceState.STATE_IN_SERVICE //这个是电信卡具体的检测有无服务
这样就可以解决电信卡检测无服务的问题了
网友评论