Android信号强度

时间:2015-08-03 07:58:17

标签: android telephonymanager dual-sim

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

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

任何帮助?

2 个答案:

答案 0 :(得分:5)

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

对于HTC M8上的Android 5.0,您可以尝试以下方法来获取两张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列表以实例化该类。再次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的实例。 e.g。

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

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

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

答案 1 :(得分:0)

在Android 7(N)上,应该执行以下操作来创建与特定订阅ID关联的TelephonyManager:

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

在Android 5.1(L MR1 / 22)到6(M / 23)上,可以在PhoneStateListner构造函数中做到这一点:

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

这两种方法都需要READ_PHONE_STATE权限。

相关问题