Android:获取4G模块卡运营商、网络类型、数据连接状态和信号格数

代码

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    String getProvider(@NonNull TelephonyManager mTelephonyManager) {
        String IMSI = "";
        @SuppressLint("MissingPermission")
        String subscriberid = mTelephonyManager.getSubscriberId();
        if (subscriberid != null) {
            IMSI = subscriberid;
        } else {
            IMSI = mTelephonyManager.getSimOperator();
        }

        String ProvidersName = "Unknown";
        if (IMSI != null && 0 != IMSI.compareTo("")) {
            if (IMSI.startsWith("46000") || IMSI.startsWith("46002") || IMSI.startsWith("46007") || IMSI.startsWith("46004")) {
                ProvidersName = "中国移动";
            } else if (IMSI.startsWith("46001") || IMSI.startsWith("46006")) {
                ProvidersName = "中国联通";
            } else if (IMSI.startsWith("46003") || IMSI.startsWith("46011")) {
                ProvidersName = "中国电信";
            } else
                ProvidersName = IMSI;
        } else {
        }

        return ProvidersName;
    }

    String getDataActivity(@NonNull TelephonyManager mTelephonyManager) {
        int activity = mTelephonyManager.getDataActivity();
        if (activity == TelephonyManager.DATA_ACTIVITY_NONE) {
            return "数据连接状态:活动,但无数据发送和接受\n";
        } else if (activity == TelephonyManager.DATA_ACTIVITY_IN) {
            return "数据连接状态:活动,正在接受数据\n";
        } else if (activity == TelephonyManager.DATA_ACTIVITY_OUT) {
            return "数据连接状态:活动,正在发送数据\n";
        } else if (activity == TelephonyManager.DATA_ACTIVITY_INOUT) {
            return "数据连接状态:活动,正在接受和发送数据\n";
        } else {
            return "数据连接状态:未知\n";
        }

    }

    String getCellularType(@NonNull TelephonyManager mTelephonyManager) {
        String cellularType = "";

        int nSubType = mTelephonyManager.getNetworkType();
        if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_1xRTT
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                || nSubType == TelephonyManager.NETWORK_TYPE_GSM
                || nSubType == TelephonyManager.NETWORK_TYPE_IDEN) {
            cellularType = "2G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_TD_SCDMA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_B
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_HSUPA
                || nSubType == TelephonyManager.NETWORK_TYPE_HSPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_A
                || nSubType == TelephonyManager.NETWORK_TYPE_HSPAP) {
            cellularType = "3G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                || nSubType == TelephonyManager.NETWORK_TYPE_IWLAN) {
            cellularType = "4G";
//        } else if (nSubType == TelephonyManager.NETWORK_TYPE_NR) {
//            cellularType= "5G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UNKNOWN) {
            cellularType = "0G";
        } else
            cellularType = String.valueOf(nSubType);

        return cellularType;
    }

    private PhoneStateListener listener = new PhoneStateListener() {
        @Override
        public void onSignalStrengthsChanged(@NonNull SignalStrength signalStrength) {

            Log.i("alderaan", "---------------------------\r\n");

            TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

            String providername = getProvider(mTelephonyManager);
            Log.i("alderaan", "运营商为:" + providername);

            String cellulartype = getCellularType(mTelephonyManager);
            Log.i("alderaan", "网络类型为:" + cellulartype);

            String activity = getDataActivity(mTelephonyManager);
            Log.i("alderaan", activity);

            //通过方法反射调用,获取出和系统一样的信号格数
            Method method1 = null;
            try {
                method1 = signalStrength.getClass().getMethod("getLteLevel");
                int level = (int) method1.invoke(signalStrength);

                if (level != 0) {
                    Log.i("alderaan", "当前手机的LTE信号格数:" + level);
                } else {
                    // LTE信号为0时,获取GSM信号强度
                    Method method2 = signalStrength.getClass().getMethod("getGsmLevel");
                    int level2 = (int) method2.invoke(signalStrength);

                    Log.i("alderaan", "当前手机的GSM信号格数:" + level2);
                }

            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            super.onSignalStrengthsChanged(signalStrength);
        }
    };

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

        Context context = this.getApplicationContext();

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        tm.listen(listener, 290);

    }
}

参考来源

使用TelephonyManager获取移动网络信息
Android 获取4G信号强度的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值