1

有什么方法可以获取两张 sim 卡上的信号强度。我搜索了很多,但找不到任何解决方案。也许有什么方法可以在第二张 SIM 卡上注册接收器?我正在使用 Android 5.0,我知道在这个版本上,Android 官方不支持双卡解决方案。我发现只有这个几乎适合我: 检查手机是否是双卡 Android 双卡信号强度

第二个链接提供了某种方式,但我不能使用它,因为方法TelephonyManager.listenGemini不可用

有什么帮助吗?

4

2 回答 2

6

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

对于 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. 例如

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

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

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

于 2015-10-01T08:26:41.837 回答
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 权限。

于 2020-10-20T13:33:53.130 回答