我正在使用TelephonyManager.getAllCellInfo()查找邻居小区rssi信号,该信号返回CellInfo(为不同的蜂窝塔类型、gsm、lte、cdma ecc.扩展的抽象类)对象列表,其中包含我所需的所有信息。
我的应用程序是使用安卓API 17级构建的,运行良好。
当我在刚刚升级到Android4.3(API级别18)的新手机上运行该应用程序时,调用getAllCellInfo()返回CellInfoWcdma(API级别18中添加的新类)的列表。
由于几个原因,我无法升级我的sdk。
如何在不升级sdk的情况下从此类对象中获取cell id和rssi?
谢谢!
使用反射:
private static class CellIdentityWcdma {
private static Class<?> mCls;
private static Method mGetCid;
private static Method mGetLac;
private static Method mGetMcc;
private static Method mGetMnc;
private static Method mGetPsc;
static {
try {
mCls = Class.forName("android.telephony.CellIdentityWcdma");
mGetCid = mCls.getMethod("getCid");
mGetLac = mCls.getMethod("getLac");
mGetMcc = mCls.getMethod("getMcc");
mGetMnc = mCls.getMethod("getMnc");
mGetPsc = mCls.getMethod("getPsc");
} catch (final ClassNotFoundException e) {
} catch (final NoSuchMethodException e) {
}
}
private final Object mObj;
public CellIdentityWcdma(final Object obj) {
mObj = obj;
}
public int getCid() throws IllegalAccessException, InvocationTargetException {
return ((Integer)mGetCid.invoke(mObj)).intValue();
}
public int getLac() throws IllegalAccessException, InvocationTargetException {
return ((Integer)mGetLac.invoke(mObj)).intValue();
}
public int getMcc() throws IllegalAccessException, InvocationTargetException {
return ((Integer)mGetMcc.invoke(mObj)).intValue();
}
public int getMnc() throws IllegalAccessException, InvocationTargetException {
return ((Integer)mGetMnc.invoke(mObj)).intValue();
}
public int getPsc() throws IllegalAccessException, InvocationTargetException {
return ((Integer)mGetPsc.invoke(mObj)).intValue();
}
}
private static class CellSignalStrengthWcdma {
private static Class<?> mCls;
private static Method mGetAsuLevel;
static {
try {
mCls = Class.forName("android.telephony.CellSignalStrengthWcdma");
mGetAsuLevel = mCls.getMethod("getAsuLevel");
} catch (final ClassNotFoundException e) {
} catch (final NoSuchMethodException e) {
}
}
private final Object mObj;
public CellSignalStrengthWcdma(final Object obj) {
mObj = obj;
}
public int getAsuLevel() throws IllegalAccessException, InvocationTargetException {
return ((Integer)mGetAsuLevel.invoke(mObj)).intValue();
}
}
private static class CellInfoWcdma {
private static Class<?> mCls;
private static Method mGetCellIdentity;
private static Method mGetCellSignalStrength;
static {
try {
mCls = Class.forName("android.telephony.CellInfoWcdma");
mGetCellIdentity = mCls.getMethod("getCellIdentity");
mGetCellSignalStrength = mCls.getMethod("getCellSignalStrength");
} catch (final ClassNotFoundException e) {
} catch (final NoSuchMethodException e) {
}
}
private final Object mObj;
public CellInfoWcdma(final Object obj) {
mObj = obj;
}
public static boolean isInstance(final Object obj) {
return null != mCls && mCls.isInstance(obj);
}
public CellIdentityWcdma getCellIdentity()
throws IllegalAccessException, InvocationTargetException {
return new CellIdentityWcdma(mGetCellIdentity.invoke(mObj));
}
public CellSignalStrengthWcdma getCellSignalStrength()
throws IllegalAccessException, InvocationTargetException {
return new CellSignalStrengthWcdma(mGetCellSignalStrength.invoke(mObj));
}
}
private static void useCellInfo(final CellInfo cellInfo) {
if (cellInfo instanceof CellInfoGsm) {
useGsm((CellInfoGsm)cellInfo);
} else if (cellInfo instanceof CellInfoLte) {
useLte((CellInfoLte)cellInfo);
} else if (cellInfo instanceof CellInfoCdma) {
useCdma((CellInfoCdma)cellInfo);
} else if (CellInfoWcdma.isInstance(cellInfo)) {
useWcdma(new CellInfoWcdma(cellInfo));
}
}