Android 获取4G信号强度的方法

时间:2025-01-29 07:36:18

仅备份

package .monitor4g;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;


import ;

import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

public class MainService extends Service {
    private static final String TAG = "MainService";
    public static String ver = "20201121";

    public static MainService Instatnce = null;
    TelephonyManager mTelephonyManager;
    MyPhoneStateListener myListener;

    public MainService() {
        ("MainService()","MainService() 被调用");
        Instatnce = this;
    }

    @Override
    public void onCreate(){
        (TAG, "onCreate is call. ver:" + ver);
        ().setContext(this);
        mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        if( .SDK_INT <25 )
        {
            myListener = new MyPhoneStateListener();//5.1只能用这个
            (null, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
            (myListener, PhoneStateListener.LISTEN_NONE);
            (myListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }else{
            //7.1 这个才是对的 5.1返回空
            new Thread(new Runnable() {
                @Override
                public void run() {
                    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    while (true){
                        @SuppressLint("MissingPermission") List<CellInfo> cellInfoList = ();
                        if(cellInfoList != null){
                            for (CellInfo cellInfo : cellInfoList)
                            {
                                if (cellInfo instanceof CellInfoLte)
                                {
                                    //cast to CellInfoLte and call all the CellInfoLte methods you need
                                    int dbm = ((CellInfoLte)cellInfo).getCellSignalStrength().getDbm();
                                    int asu = ((CellInfoLte)cellInfo).getCellSignalStrength().getAsuLevel();

                                    String provider = getProvider();
                                    String celluarType = getCellularType();
                                    String logTxt = ("%s %s dbm:%d asu:%d \n", provider, celluarType, dbm, asu);
                                    logFile(logTxt, true);
                                    ("mb_signal info",provider + " " + celluarType+ " dbm:" + dbm +" asu:" + asu);

                                    break;
                                }
                            }
                        }

                        try {
                            (1000 );
                        } catch (InterruptedException e) {
                            ();
                        }
                    }
                }
            }).start();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        ();

    }

    //输出调试文本
    public static void logFile(String txt, boolean genDate) {
        String final_txt = "";
        if (genDate) {
            SimpleDateFormat dateformat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
            String dateStr = (());
            final_txt = dateStr;
        }

        try {
            FileOutputStream outSTr = new FileOutputStream(new File("/sdcard/Download/"), true);
            BufferedOutputStream Buff = new BufferedOutputStream(outSTr);
            (final_txt.getBytes());
            (());
            ();
            ();
        } catch (IOException e) {
            ();
        }
    }

    String getProvider(){
        @SuppressLint("MissingPermission")
        String IMSI = ();
        //("qweqwes", "运营商代码" + IMSI);
        String ProvidersName = "Unknown";
        if (IMSI != null) {
            if (("46000") || ("46002") || ("46007") || ("46004")) {
                ProvidersName = "中国移动";
            } else if (("46001") || ("46006")) {
                ProvidersName = "中国联通";
            } else if (("46003") || ("46011")) {
                ProvidersName = "中国电信";
            } else
                ProvidersName = IMSI;
        } else {
        }

        return ProvidersName;
    }

    @SuppressLint("MissingPermission")
    String getCellularType(){
        String cellularType="";
        int nSubType = ();
        if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                || nSubType == TelephonyManager.NETWORK_TYPE_GSM) {
            cellularType = "2G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0) {
            cellularType= "3G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_LTE) {
            cellularType= "4G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_NR) {
            cellularType= "5G";
        }else  if(nSubType == TelephonyManager.NETWORK_TYPE_UNKNOWN){
            cellularType= "0G";
        }else
           cellularType = ( nSubType );

        return cellularType;
    }

    //5.1用这个
    private class MyPhoneStateListener extends PhoneStateListener {

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            (signalStrength);

            try {
                Method method = ().getMethod("getDbm");
                int dbm = (int) (signalStrength);

                Method method1 = ().getMethod("getAsuLevel");
                int asu = (int) (signalStrength);

                String provider = getProvider();
                String celluarType = getCellularType();
                String logTxt = ("%s %s dbm:%d asu:%d \n", provider, celluarType, dbm, asu);
                logFile(logTxt, true);
                ("mb_signal info",provider + " " + celluarType+ " dbm:" + dbm +" asu:" + asu);

                //通过这个方法能获取出和系统一样的信号格数
                //Method method2 = ().getMethod("getLteLevel");
                //int level = (signalStrength);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                ();
                ("TAG", "onSignalStrengthsChanged: 獲取4G訊號強度值失敗");
            }
        }

        @Override
        public void onServiceStateChanged(ServiceState serviceState) {
            int state = ();
            if( state == ServiceState.STATE_EMERGENCY_ONLY){
                ("手机网络状态","STATE_EMERGENCY_ONLY");
                logFile("手机网络状态:STATE_EMERGENCY_ONLY\n", true);
            }else if( state == ServiceState.STATE_OUT_OF_SERVICE){
                ("手机网络状态","STATE_OUT_OF_SERVICE");
                logFile("手机网络状态:STATE_OUT_OF_SERVICE\n", true);
            }else if( state == ServiceState.STATE_IN_SERVICE){
                ("手机网络状态", "STATE_IN_SERVICE" );
                logFile("手机网络状态:STATE_IN_SERVICE\n", true);
            }else if(state == ServiceState.STATE_POWER_OFF)
            {
                ("手机网络状态", "STATE_POWER_OFF" );
                logFile("手机网络状态:STATE_POWER_OFF\n", true);
            }else{
                ("手机网络状态", "" + state);
                logFile("手机网络状态:" + state+"\n", true);
            }
        }
    }

}

既非原创也非抄袭。本人只做搬运和融合。特此声明

引用地址:

https:///content/

/zjy764219923/article/details/104944238

https:///article/