获取GPS信号状态

locationManager = (LocationManager)LocationActivity.this.getSystemService(Context.LOCATION_SERVICE);
                locationManager.addGpsStatusListener(statusListener); 
/** 
     * 卫星状态监听器 
     */   
    private List numSatelliteList = new ArrayList(); // 卫星信号   
    private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {   
        public void onGpsStatusChanged(int event) { // GPS状态变化时的回调,如卫星数   
            LocationManager locationManager = (LocationManager) LocationActivity.this.getSystemService(Context.LOCATION_SERVICE);   
            GpsStatus status = locationManager.getGpsStatus(null); //取当前状态   
            String satelliteInfo = updateGpsStatus(event, status);   
            if("".equals(satelliteInfo)){ 
                Toast.makeText(LocationActivity.this, "GPS works bad !", Toast.LENGTH_LONG).show(); 
                isGpsEnable=false; 
            } 
        }   
    };   
    private String updateGpsStatus(int event, GpsStatus status) {   
        StringBuilder sb2 = new StringBuilder("");   
        if (status == null) {   
            sb2.append("搜索到卫星个数:" +0);   
        } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {   
            int maxSatellites = status.getMaxSatellites();   
            Iterator it = status.getSatellites().iterator();   
            numSatelliteList.clear();   
            int count = 0;   
            while (it.hasNext() && count <= maxSatellites) {   
                GpsSatellite s = it.next();   
                numSatelliteList.add(s);   
                count++;   
            }   
            sb2.append("搜索到卫星个数:" + numSatelliteList.size());   
        }   
           
        return sb2.toString();   
    } 

要是GpsSatellite数量是0,就是完全无信号。
但实际上小于4就定位不了。在网上看到的这个还有待测试

转载于:https://www.cnblogs.com/wy282904170/p/3156574.html

你可能感兴趣的:(获取GPS信号状态)