且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的信号强度

更新时间:2021-10-26 22:24:15

请注意:以下是具体一些的Andr​​oid 5.0设备。它采用隐藏式接口的是Android 5.0,并不会在早期以后版本。尤其是,订阅ID更改从 INT 当API中API 22(暴露,而您应该用官方的API反正)。

Please note: the following is specific to some Android 5.0 devices. It uses hidden interface in Android 5.0, and will not work in earlier AND later versions. In particular, the subscription id changed from long to int when the API is exposed in API 22 (for which you should use the official API anyway).

有关的Andr​​oid 5.0在HTC M8,您可以尝试下面让两个SIM卡的信号强度:

For Android 5.0 on HTC M8, you may try the following to get the signal strength of both sim cards:

重写 PhoneStateListener 及其保护的内部变量长mSubId 。由于受保护的变量是隐藏的,你需要使用反射。

Overriding PhoneStateListener and its protected internal variable long mSubId. Since the protected variable is hidden you will need to use reflection.

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
    }

}

您还需要获得积极认购的ID从 SubscriptionManager 的列表中实例化类。再次 SubscriptionManager 隐藏在5.0。

You also need to get the list of active subscription IDs from the SubscriptionManager for instantiating the class. Again SubscriptionManager is hidden in 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 的实例。例如。

Then you can iterate through the subIdList to create instances of MultiSimListener. e.g.

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

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

You can then call TelephonyManager.listen as usual, for each of the listeners.

您需要将错误和Android版本/设备检查添加到code,因为它仅适用于特定设备/版本。

You will need to add error and Android version / device check to the code, as it works only on specific devices / version.