gpt4 book ai didi

安卓信号强度

转载 作者:行者123 更新时间:2023-11-29 14:47:06 25 4
gpt4 key购买 nike

有什么方法可以在两张 SIM 卡上获得信号强度。我搜索了很多,但找不到任何解决方案。也许有什么方法可以在第二张 SIM 卡上注册接收器?我在 Android 5.0 上工作,我知道这个版本的 Android 官方不支持双卡解决方案。我发现只有这个几乎适合我: Check whether the phone is dual SIM Android dual SIM signal strength

第二个链接提供了一些方法,但我无法使用它,因为方法 TelephonyManager.listenGemini 不可用

有什么帮助吗?

最佳答案

请注意:以下内容特定于某些 Android 5.0 设备。它在 Android 5.0 中使用隐藏界面,在早期以后的版本中将无法使用。特别是,当 API 在 API 22 中公开时,订阅 ID 从 long 更改为 int(无论如何您都应该使用官方 API)。

对于 HTC M8 上的 Android 5.0,您可以尝试以下方法获取两张 sim 卡的信号强度:

重写 PhoneStateListener 及其 protected 内部变量 long mSubId。由于 protected 变量是隐藏的,因此您需要使用反射。

public class MultiSimListener extends PhoneStateListener {

private Field subIdField;
private long subId = -1;

public MultiSimListener (long subId) {
super();
try {
// Get the protected field mSubId of PhoneStateListener and set it
subIdField = this.getClass().getSuperclass().getDeclaredField("mSubId");
subscriptionField.setAccessible(true);
subscriptionField.set(this, subId);
this.subId = subId;
} catch (NoSuchFieldException e) {

} catch (IllegalAccessException e) {

} catch (IllegalArgumentException e) {

}
}

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// Handle the event here, subId indicates the subscription id if > 0
}

}

您还需要从 SubscriptionManager 获取 Activity 订阅 ID 列表以实例化该类。 SubscriptionManager 再次隐藏在 5.0 中。

final Class<?> tmClassSM = Class.forName("android.telephony.SubscriptionManager");
// Static method to return list of active subids
Method methodGetSubIdList = tmClassSM.getDeclaredMethod("getActiveSubIdList");
long[] subIdList = (long[])methodGetSubIdList.invoke(null);

然后您可以遍历 subIdList 以创建 MultiSimListener 的实例。例如

MultiSimListener listener[subIdList[i]] = new MultiSimListener(subIdList[i]);

然后您可以像往常一样为每个监听器调用 TelephonyManager.listen

您需要在代码中添加错误和 Android 版本/设备检查,因为它仅适用于特定设备/版本。

关于安卓信号强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31782191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com