Android信号强度

Android signal strength

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

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

有什么帮助吗?

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

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

覆盖 PhoneStateListener 及其受保护的内部变量 long mSubId。由于受保护的变量是隐藏的,因此您需要使用反射。

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 获取活动订阅 ID 列表以实例化 class。 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 版本/设备检查,因为它仅适用于特定设备/版本。

在 Android 7 (N) 上,应该按照以下步骤创建与特定订阅 ID 关联的 TelephonyManager:

TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager = telephonyManager.createForSubscriptionId( subId );

On Android 5.1 (L MR1 / 22) up-to 6 (M / 23),可以在 PhoneStateListner 构造函数中这样做:

try
{
    Field f = PhoneStateListener.class.getDeclaredField("mSubId");
    f.setAccessible(true);
    f.set(this, id);
}
catch (Exception e) { }

这两种方法都需要 READ_PHONE_STATE 许可。