Android获取电量、Wifi信号、手机信号
public class SystemUtil {
    /*
    *获取电量 context为UnityPlayer.currentActivity 。
     */
    public static float MonitorBatteryState(Context context)
    {
        IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent intent = context.registerReceiver(null, iFilter);
        int rawLevel = intent.getIntExtra("level", 0);      //获得当前电量
        int scale = intent.getIntExtra("scale", 0);         //获得总电量
        //int status = intent.getIntExtra("status", 0);       //电池充电状态
        return (float)rawLevel / (float)scale;
    }

    //获取Wifi信号强度 context为UnityPlayer.currentActivity 。
    @SuppressWarnings("deprecation")
    public static int ObtainWifiInfo(Context context)
    {
        int strength = 0;
        WifiManager wifiManager = (WifiManager)context.getSystemService(WIFI_SERVICE);
        WifiInfo info = wifiManager.getConnectionInfo();
        if(info.getBSSID() != null)
        {
            //链接信号强度
            strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
        }
        return  strength;
    }

    //获取手机网络信号 context为UnityPlayer.currentActivity 。
    public static int getMobileSignLevel(Context context)
    {
        int dbm = -1;
        int level = 0;
        TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        List<CellInfo> cellInfoList = tm.getAllCellInfo();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        {
            if (null != cellInfoList)
            {
                for (CellInfo cellInfo : cellInfoList)
                {
                    if (cellInfo instanceof CellInfoGsm)
                    {
                        CellSignalStrengthGsm cellSignalStrengthGsm = ((CellInfoGsm)cellInfo).getCellSignalStrength();
                        level = cellSignalStrengthGsm.getLevel();
                    }
                    else if (cellInfo instanceof CellInfoCdma)
                    {
                        CellSignalStrengthCdma cellSignalStrengthCdma = ((CellInfoCdma)cellInfo).getCellSignalStrength();
                        level = cellSignalStrengthCdma.getLevel();
                    }
                    else if (cellInfo instanceof CellInfoWcdma)
                    {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
                        {
                            CellSignalStrengthWcdma cellSignalStrengthWcdma = ((CellInfoWcdma)cellInfo).getCellSignalStrength();
                            level =  cellSignalStrengthWcdma.getLevel();
                        }
                    }
                    else if (cellInfo instanceof CellInfoLte)
                    {
                        CellSignalStrengthLte cellSignalStrengthLte = ((CellInfoLte)cellInfo).getCellSignalStrength();
                        level = cellSignalStrengthLte.getLevel();
                    }
                }
            }
        }

        return level;
    }
}

首页 我的博客
粤ICP备17103704号