尊重原作,此文转载于https://www.cnblogs.com/ldq2016/p/7028488.html,感谢作者提供思路。
需求:检测双卡的信号强度和状态
解决方案:
- 检测sim卡1
private void sim1() {
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
SubscriptionManager mSubscriptionManager = SubscriptionManager.from(this);
SubscriptionInfo sub0 = mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(0);
if (null != sub0) {
//如果不为空 说明sim卡1存在
/*
sub0.getSubscriptionId() 获取sim卡1的 subId
*/
Sim1SignalStrengthsListener mSim1SignalStrengthsListener = new Sim1SignalStrengthsListener(sub0.getSubscriptionId());
//开始监听
if (mTelephonyManager != null) {
mTelephonyManager.listen(mSim1SignalStrengthsListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
}
boolean isSimCardExist = false;
try {
Method method = TelephonyManager.class.getMethod("getSimState", int.class);
int simState = (Integer) method.invoke(mTelephonyManager, new Object[]{0});
if (TelephonyManager.SIM_STATE_READY == simState) {
isSimCardExist = true;
}
} catch (Exception e) {
e.printStackTrace();
}
Log.i(TAG,"isSimCardExist:" + isSimCardExist);
}
这里直接返回了isSimCardExist的状态,通过这个可以判断sim卡是否可用。
信号强度则是由Sim1SignalStrengthsListener类返回,接下来看Sim1SignalStrengthsListener类代码:
public class Sim1SignalStrengthsListener extends PhoneStateListener {
public Sim1SignalStrengthsListener(int subId) {
super();
ReflectUtil.setFieldValue(this, "mSubId", subId);
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
int level = getSignalStrengthsLevel(signalStrength);
//level返回的即是信号强度0-5
Log.i(TAG,"level:" + level);
}
private int getSignalStrengthsLevel(SignalStrength signalStrength) {
int level = -1;
try {
Method levelMethod = SignalStrength.class.getDeclaredMethod("getLevel");
level = (int) levelMethod.invoke(signalStrength);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return level;
}
}
ReflectUtil反射类的代码:
public class ReflectUtil {
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
Field field = getAccessibleField(obj, fieldName);
if (field == null) {
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
}
try {
field.set(obj, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static Field getAccessibleField(final Object obj, final String fieldName) {
if (obj == null) {
throw new IllegalArgumentException("object can't be null");
}
if (fieldName == null || fieldName.length() <= 0) {
throw new IllegalArgumentException("fieldName can't be blank");
}
for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
try {
Field field = superClass.getDeclaredField(fieldName);
makeAccessible(field);
return field;
} catch (NoSuchFieldException e) {
continue;
}
}
return null;
}
public static void makeAccessible(Field field) {
if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
field.setAccessible(true);
}
}
}
照搬照抄测试一下结果如下:
08-28 15:48:24.891 8205-8205/com.example.gqiu.helloworld I/lgq: isSimCardExist:true
08-28 15:48:24.896 8205-8205/com.example.gqiu.helloworld I/lgq: sim2 isSimCardExist:true
08-28 15:48:24.928 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:24.929 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:25.285 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:28.291 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:28.330 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:31.297 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:31.334 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:34.304 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:34.339 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:37.329 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:37.352 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:40.332 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:40.370 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
- sim2检测
//此处0修改为1,其他同sim1,就不必废话了。祝君好运~
SubscriptionInfo sub0 = mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1);