How to switch provider into GPS_provider if androi

2019-08-23 13:14发布

问题:

i make a location application on android. here's the code for acquiring lat and long value.

public void geoLocation()
    {
        locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
                updateWithNewLocation(location);
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {
                Toast.makeText(ListenSMSservice.this,"Network Enabled",Toast.LENGTH_SHORT ).show(); 
            }
            public void onProviderDisabled(String provider) {
                Toast.makeText(ListenSMSservice.this,"Network Disabled",Toast.LENGTH_SHORT ).show();
            }
          };
        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    public void updateWithNewLocation(Location location) //update posisi terkini
    {
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();

            Geocoder gc = new Geocoder(ListenSMSservice.this, Locale.getDefault());
            try {
              List<Address> addresses = gc.getFromLocation(lat, lng, 1);
              StringBuilder sb = new StringBuilder();
              if (addresses.size() > 0) {
                Address address = addresses.get(0);

                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                  sb.append(address.getAddressLine(i)).append("\n");

                  sb.append(address.getLocality()).append("\n");
                  sb.append(address.getCountryName());
              }
              addressString = sb.toString();
            } catch (IOException e) {}

            //latLongString = "Lat:" + lat + "\nLong:" + lng;     
            latLongUrl="Phone Map\nhttp://maps.google.com/maps?q=" + lat + ",+" + lng + "+(Your+phone+location)&iwloc=A&hl=en\n";
          } else {
            latLongUrl = "No location found"; 
          }
          myMessage=latLongUrl+"\n"+addressString;
          locationManager.removeUpdates(locationListener); 
          locationManager = null;
          //Toast.makeText(ListenSMSservice.this, myMessage, 3).show();
          sendsms();
    }

that code will works if i have an internet connection (NETWORK_PROVIDER). has anyone know how to modify that code for automatically switch into GPS_PROVIDER when android phone doesn't have any internet connection?? thanks

回答1:

onLocationUpdate() just check for Internet is available or not,

If Internet is not available then just,

   locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

so like this,

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
    return false;
}

EDIT: Here you can find beautiful explanation about Hoe to get current location,

What is the simplest and most robust way to get the user's current location in Android?

please refer this also.

Thanks