如何在Android中收听双SIM卡信号电平?

时间:2019-05-12 04:48:58

标签: java android dual-sim

我正在开发android启动器应用程序,并希望在我的应用程序中支持双SIM卡信号电平条。

package com.example.sms;

import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private TextView tvSim1SignalLevel, tvSim2SignalLevel;
    private TelephonyManager telephonyManager;
    private SignalLevelListener signalLevelListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvSim1SignalLevel = findViewById(R.id.tvSim1SignalLevel);
        tvSim2SignalLevel = findViewById(R.id.tvSim2SignalLevel);

        signalLevelListener = new SignalLevelListener();
        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(signalLevelListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    public class SignalLevelListener extends PhoneStateListener {
        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);

            tvSim1SignalLevel.setText(String.valueOf(signalStrength.getLevel()));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        telephonyManager.listen(signalLevelListener, PhoneStateListener.LISTEN_NONE);
    }
}

我仅获得默认的SIM信号电平,如何获得第二个SIM信号电平,如状态栏sims信号电平图标中一样?

1 个答案:

答案 0 :(得分:0)

使用遥测信号的双Sim信号

  fun getRegisteredCellInfo(cellInfos: MutableList<CellInfo>): ArrayList<CellInfo> {
    val registeredCellInfos = ArrayList<CellInfo>()
    if (cellInfos.isNotEmpty()) {
        for (i in cellInfos.indices) {
            if (cellInfos[i].isRegistered) {
                registeredCellInfos.add(cellInfos[i])
            }
        }
    }
    return registeredCellInfos
}


fun getNetworkStrength(): Pair<Int, Int> {

    var strength1 = -1
    var strength2 = -1


    val manager = getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        val telephonyManager = applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager


        if (telephonyManager.allCellInfo != null) {


            val allCellinfo = telephonyManager.allCellInfo
            val activeSubscriptionInfoList = manager.activeSubscriptionInfoList

           val regCellInfo= getRegisteredCellInfo(allCellinfo)

                activeSubscriptionInfoList.forEachIndexed { Subindex, subs ->

                    if (activeSubscriptionInfoList.size >= 2) {

                        if (regCellInfo.size >= 2) {

                            if (subs.simSlotIndex == 0) {

                                if (subs.carrierName != "No service") {


                                    strength1 = when (val info1 = regCellInfo[0]) {
                                        is CellInfoLte -> info1.cellSignalStrength.dbm
                                        is CellInfoGsm -> info1.cellSignalStrength.dbm
                                        is CellInfoCdma -> info1.cellSignalStrength.dbm
                                        is CellInfoWcdma -> info1.cellSignalStrength.dbm
                                        else -> 0
                                    }

                                    Timber.i("subs $subs")

                                    Timber.i("sim1   ${subs.carrierName}  ${subs.mnc}  $strength1")
                                } else {

                                    strength1 = -1
                                }

                            } else if (subs.simSlotIndex == 1) {

                                if (subs.carrierName != "No service") {

                                    strength2 = when (val info2 = regCellInfo[1]) {
                                        is CellInfoLte -> info2.cellSignalStrength.dbm
                                        is CellInfoGsm -> info2.cellSignalStrength.dbm
                                        is CellInfoCdma -> info2.cellSignalStrength.dbm
                                        is CellInfoWcdma -> info2.cellSignalStrength.dbm
                                        else -> 0
                                    }

                                    Timber.i("sim2   ${subs.carrierName}  $strength2")
                                } else {

                                    strength2 = -1
                                }

                            }

                        }
                    }else if(activeSubscriptionInfoList.size == 1)
                    {

                        if(regCellInfo.size >= 1) {

                            if (subs.simSlotIndex == 0) {

                                if (subs.carrierName != "No service") {


                                    strength1 = when (val info1 = regCellInfo[0]) {
                                        is CellInfoLte -> info1.cellSignalStrength.level
                                        is CellInfoGsm -> info1.cellSignalStrength.level
                                        is CellInfoCdma -> info1.cellSignalStrength.level
                                        is CellInfoWcdma -> info1.cellSignalStrength.level
                                        else -> 0
                                    }

                                    Timber.i("subs $subs")

                                    Timber.i("sim1   ${subs.carrierName}  ${subs.mnc}  $strength1")
                                } else {

                                    strength1 = -1
                                }

                            }
                        }

                        strength2 = -2

                    }
                }

            }
        }

    Timber.i("final strenght   sim1 $strength1  sim2 $strength2")

    return Pair(strength1, strength2)
}