1.开机时,获取4G网络强度时间慢
2.底层显示4G 网路链接成功了,但是无法显示网络强度和网络图标
3.客户需要连接WIFI 后,需要显示4G 网络强度
当时处理这几个问题我花一个多月的时间,主要是要理清android 4G网络是怎么个执行流程,你才能更好的处理这些问题。
1.===>网络强度加载慢原因:开机默认获取网络强度值为0,只有监听到网络强度变化时,才会将网络强度值更新。所以网络强度加载慢
//下面网络强度加载慢打印
D/==bobe===( 606): updateTelephonySignalStrength: hasService=true ss=SignalStrength: 99 -1 -1 -1 -1 -1 -1 99 2147483647 2147483647 2147483647 2147483647 gsm|lte
D/==bobe===( 606): ====BOBE====mCurrentState.connected: true
D/==bobe===( 606): ====BOBE====hasService(): true
D/==bobe===( 606): ====BOBE====mSignalStrength: SignalStrength: 99 -1 -1 -1 -1 -1 -1 99 2147483647 2147483647 2147483647 2147483647 gsm|lte
D/==bobe===( 606): ===22==mSignalStrength.getCdmaLevel:=====0
//获取到网络强度的打印
D/==bobe===( 606): ====BOBE====mSignalStrength: SignalStrength: 25 99 -85 -85 -85 -85 -1 99 2147483647 2147483647 2147483647 2147483647 gsm|lte
D/SignalStrength( 606): =====getLevel()=======
D/SignalStrength( 606): =====getLteLevel()=====
D/SignalStrength( 606): =====getGsmLevel()=======
D/==bobe===( 606): ===22==mSignalStrength.getCdmaLevel:=====4
处理办法:我将默认网络强度改为24,也就是开机启动后获取到默认的网络强度为24,但是当监听到网络强度有变化时,他也会更新网络强度的值。
修改地方:
+++ b/frameworks/base/telephony/java/android/telephony/SignalStrength.java
@@ -95,12 +95,13 @@ public class SignalStrength implements Parcelable {
* @hide
*/
public SignalStrength() {
- mGsmSignalStrength = 99;
- mGsmBitErrorRate = -1;
- mCdmaDbm = -1;
- mCdmaEcio = -1;
- mEvdoDbm = -1;
- mEvdoEcio = -1;
+
+ mGsmSignalStrength = 24;
+ mGsmBitErrorRate = 99;
+ mCdmaDbm = -87;
+ mCdmaEcio = -87;
+ mEvdoDbm = -87;
+ mEvdoEcio = -87;
2.==>4G网络类型和网络强度无法加载原因:
出现问题打印:
D/NetworkController.MobileSignalController(1)( 607): ==onServiceStateChanged voiceState=3 dataState=3返回服务状态为3,对应的是 STATE_POWER_OFF=3,表示电话无线电明确关闭。
D/NetworkController.MobileSignalController(1)( 607): ==onDataConnectionStateChanged: state=2 type=13
D/NetworkController( 607): =========typeIcon: 0 0表示4G网络图标无法显示
D/NetworkController( 607): =========signalClustersLength: 3
D/NetworkController( 607): ==网络强度==getCurrentIconId
D/NetworkController.MobileSignalController(1)( 607): ==onSignalStrengthsChanged signalStrength=SignalStrength: 99 99 -120 -160 -120 -1 -1 24 -93 -20 2147483647 2147483647 gsm|lte level=4
正常情况打印: D/NetworkController.MobileSignalController(1)( 607): ==onServiceStateChanged voiceState=0 dataState=0
D/NetworkController.MobileSignalController(1)( 607): ==onDataConnectionStateChanged: state=2 type=13
D/NetworkController( 607): =========typeIcon: 2130837809
D/NetworkController( 607): =========signalClustersLength: 3
D/NetworkController( 607): ==网络强度==getCurrentIconId
public void onServiceStateChanged(ServiceState state) {
/**
* STATE_IN_SERVICE: 0
* STATE_OUT_OF_SERVICE: 1
* STATE_EMERGENCY_ONLY: 2
* STATE_POWER_OFF: 3
* RIL_REG_STATE_NOT_REG: 0
*/
+ state.setVoiceRegState(0);
+ state.setDataRegState(0);
Log.d(mTag, "==onServiceStateChanged voiceState:" + state.getVoiceRegState()
+ " dataState:" + state.getDataRegState());
mServiceState = state;
updateTelephony();
}
下面是返回ServiceState类型:
/**
*正常运行状态,手机已注册
*与家庭网络或漫游中的运营商。
*/
public static final int STATE_IN_SERVICE = 0;
/**
*电话未向任何运营商,电话注册
*目前可以搜索新的运营商注册或不注册
*完全搜索注册,或注册被拒绝,或无线电
*信号不可用。
*/
public static final int STATE_OUT_OF_SERVICE = 1;
/**
*手机已注册并已锁定。 只允许紧急号码。 {@更多}
*/
public static final int STATE_EMERGENCY_ONLY = 2;
/**
*电话无线电明确关闭。
*/
public static final int STATE_POWER_OFF = 3;
3.==>客户需要连接WIFI 后,需要显示4G 网络强度 ==>处理逻辑代码在SignalClusterView.java 。只要在连接WiFi后显示4G 网络强度就可以
下面一起看下代码如和执行
1. SignalClusterView.java 显示网络图标代码 ,包括4G,WIFI,ETH
2. PhoneStatusBar.java 处理4G网络 ,下面是它的代码
NetworkControllerImpl mNetworkController; 这里调用了NetworkControllerImpl.java 接口
SecurityControllerImpl mSecurityController; 这里调用了SecurityControllerImpl.java 接口
final SignalClusterView signalCluster =
(SignalClusterView) mStatusBarView.findViewById(R.id.signal_cluster);
final SignalClusterView signalClusterKeyguard =
(SignalClusterView) mKeyguardStatusBar.findViewById(R.id.signal_cluster);
final SignalClusterView signalClusterQs =
(SignalClusterView) mHeader.findViewById(R.id.signal_cluster);
mNetworkController.addSignalCluster(signalCluster);
mNetworkController.addSignalCluster(signalClusterKeyguard);
mNetworkController.addSignalCluster(signalClusterQs);
signalCluster.setSecurityController(mSecurityController);
signalCluster.setNetworkController(mNetworkController);
signalClusterKeyguard.setSecurityController(mSecurityController);
signalClusterKeyguard.setNetworkController(mNetworkController);
signalClusterQs.setSecurityController(mSecurityController);
signalClusterQs.setNetworkController(mNetworkController);
3.NetworkControllerImpl.java 网络控制实现(注册网络广播,接受广播。显示网络类型和网络强度)
注册广播:
private void registerListeners() {
for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) {
mobileSignalController.registerListener();
}
mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
// broadcasts
IntentFilter filter = new IntentFilter();
filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED); SIM状态变化
filter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
filter.addAction(TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED);
filter.addAction(TelephonyIntents.ACTION_SUBINFO_ICON_TINT_CHANGE);
filter.addAction(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE);
filter.addAction(ConnectivityManager.INET_CONDITION_ACTION);
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
filter.addAction(EthernetManager.ETHERNET_STATE_CHANGED_ACTION);
mContext.registerReceiver(this, filter);
mListening = true;
updateMobileControllers();
}
手机接受到广播后处理相应操作:
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE) ||
action.equals(ConnectivityManager.INET_CONDITION_ACTION)) {
updateConnectivity();
refreshCarrierLabel();
} else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
mConfig = Config.readConfig(mContext);
handleConfigurationChanged();
} else if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
refreshLocale();
updateAirplaneMode(false);
refreshCarrierLabel();
} else if (action.equals(TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED)) {
// We are using different subs now, we might be able to make calls.
recalculateEmergency();
} else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
// Notify every MobileSignalController so they can know whether they are the
// data sim or not.
for (MobileSignalController controller : mMobileSignalControllers.values()) {
controller.handleBroadcast(intent);
}
} else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
// Might have different subscriptions now.
//updateMobileControllers();
}
监听手机状态,会执行相应的处理方法
/**
* Start listening for phone state changes.
*/
public void registerListener() {
Log.d(TAG,"监听手机状态");
mPhone.listen(mPhoneStateListener,
PhoneStateListener.LISTEN_SERVICE_STATE 网路服务状态
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS 网络强度状态
| PhoneStateListener.LISTEN_CALL_STATE
| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE 数据连接状态
| PhoneStateListener.LISTEN_DATA_ACTIVITY);
}
监听到网络强度变化执行的方法:
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
Log.d(mTag, "==onSignalStrengthsChanged signalStrength=" + signalStrength +
((signalStrength == null) ? "" : (" level=" + signalStrength.getLevel())));
mSignalStrength = signalStrength;
updateTelephony();
}
监听到服务状态变化会执行的方法:
@Override
public void onServiceStateChanged(ServiceState state) {
/**
* STATE_IN_SERVICE: 0
* STATE_OUT_OF_SERVICE: 1
* STATE_EMERGENCY_ONLY: 2
* STATE_POWER_OFF: 3
* RIL_REG_STATE_NOT_REG: 0
*/
state.setVoiceRegState(0);
state.setDataRegState(0);
Log.d(mTag, "==onServiceStateChanged voiceState:" + state.getVoiceRegState()
+ " dataState:" + state.getDataRegState());
mServiceState = state;
updateTelephony();
}
监听到数据已经连接状态会执行方法:
@Override
public void onDataConnectionStateChanged(int state, int networkType) {
Log.d(mTag, "==onDataConnectionStateChanged: state=" + state
+ " type=" + networkType);
mDataState = state;
mDataNetType = networkType;
updateTelephony();
}
@Override
public void onDataActivity(int direction) {
if (DEBUG) {
Log.d(mTag, "onDataActivity: direction=" + direction);
}
setActivity(direction);
}
};
/**
*根据mServiceState,mSignalStrength,mDataNetType更新当前状态,
* mDataState和mSimState。 任何时候更新其中一个都应该调用它。
*如有必要,这将调用听众。
*/
private final void updateTelephony() {
Log.d(TAG,"==updateTelephony===1111=");
Log.d(TAG, "updateTelephonySignalStrength: hasService=" + hasService()
+ " ss=" + mSignalStrength);
mCurrentState.connected = hasService() && mSignalStrength != null;
Log.d(TAG,"====BOBE====mCurrentState.connected: " +mCurrentState.connected);
Log.d(TAG,"====BOBE====hasService(): " +(hasService()));
Log.d(TAG,"====BOBE====mSignalStrength: " +mSignalStrength);
if (mCurrentState.connected) {
if (!mSignalStrength.isGsm() && mConfig.alwaysShowCdmaRssi) {
mCurrentState.level = mSignalStrength.getCdmaLevel();
Log.d(TAG,"===11==mSignalStrength.getCdmaLevel:====="+mCurrentState.level);
} else {
mCurrentState.level = mSignalStrength.getLevel();
Log.d(TAG,"===22==mSignalStrength.getCdmaLevel:====="+mCurrentState.level);
}
}
if (mNetworkToIconLookup.indexOfKey(mDataNetType) >= 0) {
mCurrentState.iconGroup = mNetworkToIconLookup.get(mDataNetType);
Log.d(TAG,"=====mCurrentState.iconGroup:====="+mCurrentState.iconGroup);
} else {
mCurrentState.iconGroup = mDefaultIcons;
}
Log.d(TAG,"=====updateTelephony()=========mCurrentState.connected:"+mCurrentState.connected);
Log.d(TAG,"=====updateTelephony()=========mDataState:"+( mDataState == TelephonyManager.DATA_CONNECTED));
Log.d(TAG,"=====updateTelephony()=========mCurrentState.dataConnected:"+mCurrentState.dataConnected);
mCurrentState.dataConnected = mCurrentState.connected
&& mDataState == TelephonyManager.DATA_CONNECTED;
Log.d(TAG,"=====updateTelephony()=========mCurrentState.dataConnected:"+mCurrentState.dataConnected);
if (isRoaming()) {
mCurrentState.iconGroup = TelephonyIcons.ROAMING;
}
if (isEmergencyOnly() != mCurrentState.isEmergency) {
mCurrentState.isEmergency = isEmergencyOnly();
mNetworkController.recalculateEmergency();
}
// Fill in the network name if we think we have it.
if (mCurrentState.networkName == mNetworkNameDefault && mServiceState != null
&& mServiceState.getOperatorAlphaShort() != null) {
mCurrentState.networkName = mServiceState.getOperatorAlphaShort();
}
notifyListenersIfNecessary();
}
//
private void notifyAllListeners() {
Log.d(TAG,"===notifyAllListeners()=====");
notifyListeners();
for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) {
mobileSignalController.notifyListeners();
}
mWifiSignalController.notifyListeners();
}
//
@Override
public void notifyListeners() {
Log.d(TAG,"==notifyListeners==========111111=====");
MobileIconGroup icons = getIcons();
String contentDescription = getStringIfExists(getContentDescription());
String dataContentDescription = getStringIfExists(icons.mDataContentDescription);
Log.d(TAG,"==notifyListeners==========mCurrentState.dataConnected: "+mCurrentState.dataConnected);
Log.d(TAG,"==notifyListeners==========mCurrentState.inetForNetwork: " +mCurrentState.inetForNetwork);
Log.d(TAG,"==notifyListeners==========mCurrentState.iconGroup:=====" +(mCurrentState.iconGroup == TelephonyIcons.ROAMING));
boolean showDataIcon = mCurrentState.dataConnected && mCurrentState.inetForNetwork != 0
|| mCurrentState.iconGroup == TelephonyIcons.ROAMING;
// Only send data sim callbacks to QS.
if (mCurrentState.dataSim) {
int qsTypeIcon = showDataIcon ? icons.mQsDataType[mCurrentState.inetForNetwork] : 0;
int length = mSignalsChangedCallbacks.size();
for (int i = 0; i < length; i++) {
mSignalsChangedCallbacks.get(i).onMobileDataSignalChanged(mCurrentState.enabled
&& !mCurrentState.isEmergency,
getQsCurrentIconId(), contentDescription,
qsTypeIcon,
mCurrentState.dataConnected && mCurrentState.activityIn,
mCurrentState.dataConnected && mCurrentState.activityOut,
dataContentDescription,
mCurrentState.isEmergency ? null : mCurrentState.networkName,
// Only wide if actually showing something.
icons.mIsWide && qsTypeIcon != 0);
}
}
//是否显示4G
int typeIcon = showDataIcon ? icons.mDataType : 0;
// int typeIcon = icons.mDataType;
Log.d(TAG,"=========typeIcon: "+typeIcon);
int signalClustersLength = mSignalClusters.size();
Log.d(TAG,"=========signalClustersLength: "+signalClustersLength);
for (int i = 0; i < signalClustersLength; i++) {
mSignalClusters.get(i).setMobileDataIndicators(
mCurrentState.enabled && !mCurrentState.airplaneMode,
getCurrentIconId(),
typeIcon,
contentDescription,
dataContentDescription,
// Only wide if actually showing something.
icons.mIsWide && typeIcon != 0,
mSubscriptionInfo.getSubscriptionId());
}
}