Android 2.2之前的老API已经废除了:
com.android.internal.telephony
Class PhoneStateIntentReceiver
java.lang.Object android.content.BroadcastReceiver com.android.internal.telephony.PhoneStateIntentReceiver
Deprecated.
@Deprecated public final class PhoneStateIntentReceiver extends BroadcastReceiver
DO NOT USE THIS CLASS: Use android.telephony.TelephonyManager and PhoneStateListener instead.
最新的API使用PhoneStatListener方式:
1. 继承PhoneStateListener, 重载onCellLocationChanged,onSignalStrengthsChanged,onCellInfoChanged,这这些回调了取到你想要的信息
private class PhoneStateChangeListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
String network = getNetwork();
if (network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_CDMA])) {
setCurrentRssi(signalStrength.getCdmaDbm());
} else if (network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_EVDO_0]) ||
network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_EVDO_A]) ||
network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_EVDO_B])) {
setCurrentRssi(signalStrength.getEvdoDbm());
} else if (signalStrength.isGsm()) {
//XXX 待研究,为什么联通网络我们会走这里,取到了GSM的signalStrenght(0,99),
//而其它工具却能取到-84dBm(-100, 0)
//各种网络信号强度术语:http://stackoverflow.com/questions/15054498/android-signalstrength-terminology-explanation
setCurrentRssi(signalStrength.getGsmSignalStrength());
}
}
@Override
public void onCellInfoChanged (List<CellInfo> cellInfo) {
for(CellInfo info: cellInfo)
{
Logger.d(info.toString());
}
}
@Override
public void onCellLocationChanged (CellLocation location) {
Logger.d(location.toString());
int type = telephonyManager.getNetworkType();
Logger.d("getCellIDInfo--> NetworkType = " + type);
int phoneType = telephonyManager.getPhoneType();
Logger.d("getCellIDInfo--> phoneType = " + phoneType);
if (type == TelephonyManager.NETWORK_TYPE_GPRS // GSM网
|| type == TelephonyManager.NETWORK_TYPE_EDGE
|| type == TelephonyManager.NETWORK_TYPE_HSDPA) {
GsmCellLocation gsm = (GsmCellLocation)location;
int lac = gsm.getLac();
int cid = gsm.getCid();
//String mcc = telephonyManager.getNetworkOperator().substring(0, 3);
//String mnc = telephonyManager.getNetworkOperator().substring(3, 5);
}
else if (type == TelephonyManager.NETWORK_TYPE_CDMA // 电信cdma网
|| type == TelephonyManager.NETWORK_TYPE_1xRTT
|| type == TelephonyManager.NETWORK_TYPE_EVDO_0
|| type == TelephonyManager.NETWORK_TYPE_EVDO_A)
{
CdmaCellLocation cdma = (CdmaCellLocation)location;
int lac = cdma.getNetworkId();
int cid = cdma.getBaseStationId();
//String mcc = telephonyManager.getNetworkOperator().substring(0, 3);
//String mnc = String.valueOf(cdma.getSystemId());
}
}
}
2. 向注册TelephonyManager注册监听事件
/**
* This method must be called in the service thread, as the system will create a Looper in
* the calling thread which will handle the callbacks.
*/
public void registerPhoneStateChangeListener() {
initNetwork();
if(null == phoneStateChangeListener) {
phoneStateChangeListener = new PhoneStateChangeListener();
}
telephonyManager.listen(phoneStateChangeListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
telephonyManager.listen(phoneStateChangeListener, PhoneStateListener.LISTEN_CELL_INFO);
telephonyManager.listen(phoneStateChangeListener, PhoneStateListener.LISTEN_CELL_LOCATION);
}