一段时间后PhoneStateListener停止工作

时间:2019-06-05 13:26:19

标签: java android kotlin foreground-service phone-state-listener

我有一个前台服务,可以在其中注册两张SIM卡的侦听器,并且一切正常,但只能使用一段时间。几个小时后,MultiSimListener停止接收第一个或第二个SIM卡插槽的广播。 我以为是打a睡模式造成的,所以我使用adb启用了它,但没有任何结果-应用程序应能正常工作。

在装有Android 9的Samsung S10E上进行了测试。

任何想法是什么原因?

class AppService : Service() {
    private lateinit var multiSimListener: MutableList<MultiSimListener>
    private lateinit var telephonyManagerSub: MutableList<TelephonyManager>

    ...

    private fun myStartForegroundService() {

        ...

        val defaultTelephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val subscriptionManager = getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
        var i = 0
        telephonyManagerSub = mutableListOf()
        multiSimListener = mutableListOf()
        for (subscriptionInfo in subscriptionManager.activeSubscriptionInfoList) {
            telephonyManagerSub.add(defaultTelephonyManager.createForSubscriptionId(subscriptionInfo.subscriptionId))
            multiSimListener.add(MultiSimListener(subscriptionInfo, telephonyManagerSub[i]))
            telephonyManagerSub[i].listen(multiSimListener[i], PhoneStateListener.LISTEN_CALL_STATE)
            Log.i(TAG, "Listening for calls on subscription $subscriptionInfo")
            telephonyManagerSub[i].listen(multiSimListener[i], PhoneStateListener.LISTEN_CALL_STATE)
            i++
        }

        ...

        // Create notification, etc.

public class MultiSimListener extends PhoneStateListener {

    private final SubscriptionInfo subscriptionInfo;
    private final TelephonyManager telephonyManagerSub;

    public MultiSimListener(SubscriptionInfo subscriptionInfo, TelephonyManager telephonyManagerSub) {
        super();
        this.subscriptionInfo = subscriptionInfo;
        this.telephonyManagerSub = telephonyManagerSub;
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                Log.i(TAG, "Incoming call on SIM " + subscriptionInfo.getSimSlotIndex() + " from " + incomingNumber + ", call state=" + telephonyManagerSub.getCallState());

                CurrentSIMCard.setSim(subscriptionInfo.getSimSlotIndex());

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                break;

        }
    };
}

0 个答案:

没有答案