Why isProviderEnabled() method always return true even inside the Building

两盒软妹~` 提交于 2019-12-11 03:46:27

问题


In this case I use GPS as a Provider, I try walking inside the building but seem like it doesn't find the location. Yeah, it makes sense that in the building GPS not work. So, Why isProviderEnabled return true? Anyway, What is the way that i should implement "Searching for GPS signal"?

            String context = Context.LOCATION_SERVICE;
            locman = (LocationManager)getSystemService(context);


            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            String provider = locman.getBestProvider(criteria, true);

            if (locman.isProviderEnabled( LocationManager.GPS_PROVIDER )) {
            // Change from 'Searching for GPS signal to Use GPS'
                txtGPS.setText("Use GPS");

            }
            locman.requestLocationUpdates(
                    provider,MIN_TIME, MIN_DISTANCE, locationListener);

回答1:


isProviderEnabled() only looks if you have enabled GPS or not, it dosn't take care if you see any satellites!

If you like to check, if there are any satellites you have to use the gpslistener and this is how to use the gpslistner:

/**
         * Hier wird ein neuer GPSListener erstellt.
         * Dieser Überprüft ob es Sichtbare Sattelitten gibt.
         */
        mLocationManager.addGpsStatusListener(new GpsStatus.Listener() {

            public void onGpsStatusChanged(int event) {

                // Hier wird der EVENT gefiltert
                // Dieser EVENT erscheint immer wenn GPS Satteliten in sicht sind
                if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
                    // GpsStatus casten
                    GpsStatus status = mLocationManager.getGpsStatus(null);

                    // Interable sats instanzieren und casten
                    Iterable<GpsSatellite> sats = status.getSatellites();

                    // SATCOUNT instanzieren, wird benötigt um die Satteliten zu zählen
                    int SATCOUNT = 0;

                    // Interator satI instanzieren und casten.
                    Iterator<GpsSatellite> satI = sats.iterator();

                    // Hier wird die anzahl der Satteliten gezählt
                    while(satI.hasNext()){
                        // Den Interator auf den nächsten Satteliten verweisen
                        GpsSatellite satellite = satI.next();

                        // Satelitten zählen
                        SATCOUNT++;
                        Log.d("GPSListner", "While with SATCOUNT = " + SATCOUNT + " Elevation des Satellites = " + satellite.getElevation());


                    }

                    // Prüfen ob SATCOUNT 4 Satelitten hat
                    if(SATCOUNT > GPS_SAT_COUNT){

                    String test = String.valueOf(SATCOUNT);
                    Log.d("GPSSTATUS", "(true) Anzahl der Sattelitten: " + test );

                    // Boolean mGpsOk auf true setzen
                    GPSService.mGpsOk = true;
                    }else{

                    String test2 = String.valueOf(SATCOUNT);
                    Log.d("GPSSTATUS", "(fasle) Anzahl der Sattelitten: " + test2 );

                    // Boolean mGpsOk auf false setzen
                    GPSService.mGpsOk = false;

                    }

                }
            }
        });

To get this one working, you need to start a locationmanager which wants to use the gps, now your gpslistner can check if there are any satellites in view and writes a variable true or false. Afterwards you can check the Variabel. If its true (at least 4 Satellites in View) you can use Criteria.ACCURACY_FINE else you can use Criteria.ACCURACY_COARSE. Don't forget to set the permission for both!

Best Regards

safari



来源:https://stackoverflow.com/questions/10411998/why-isproviderenabled-method-always-return-true-even-inside-the-building

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!