Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

android - Get GPS Status on location changed

I am using GPS services to derive latitude & longitude in my App. I am able to get the latitude and longitude when the signal is avialable. Once i get a fix, if i have to go to a place where the GPS is not able to get a location fix, it still shows the last location. I want to get the current location and not otherwise. I have tried using onLocationChanged() and onGpsStatusChanged() both to get the GPS status but it isnt giving me the result.

@Override
public void onGpsStatusChanged(int arg0) {
    // TODO Auto-generated method stub
    if (event == GpsStatus.GPS_EVENT_STARTED) {
        Log.d("zmenaGPS" , "GPS event started ");
        GpsStatus gs = mlocManager.getGpsStatus(null);
        Toast.makeText(getApplicationContext(),String.valueOf(gs),Toast.LENGTH_LONG).show();

    } else if (event == GpsStatus.GPS_EVENT_STOPPED) {
        Log.d("zmenaGPS" , "GPS event stopped ");
        GpsStatus gs = mlocManager.getGpsStatus(null);
        Log.d("zmenaGPSgps status" , String.valueOf(gs));
        Toast.makeText(getApplicationContext(),String.valueOf(gs),Toast.LENGTH_LONG).show();

    } else if (event == GpsStatus.GPS_EVENT_FIRST_FIX) {
        Log.d("zmenaGPS" , "GPS fixace ");
        Toast.makeText(getApplicationContext(),"First Fix",Toast.LENGTH_LONG).show();
    } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
        Log.d("zmenaGPS" , "GPS EVET NECO ");
    } 

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use NETWORK_PROVIDER instead to get updated location all time. like this..

public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(Context.LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
//                  locationManager.requestLocationUpdates(
//                          LocationManager.NETWORK_PROVIDER,
//                          MIN_TIME_BW_UPDATES,
//                          MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled){
                    if (location == null){
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
//                      locationManager.requestLocationUpdates(
//                              LocationManager.GPS_PROVIDER,
//                              MIN_TIME_BW_UPDATES,
//                              MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
//          if (!isGPSEnabled) {
//              showSettingsAlert();
//          } 
        } catch (Exception e){
            e.printStackTrace();
        }

        return location;
    }

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...