Skip to content

Instantly share code, notes, and snippets.

@kishandonga
Created September 21, 2020 13:09
Show Gist options
  • Save kishandonga/fcbbe0f48899f763afd127682645218c to your computer and use it in GitHub Desktop.
Save kishandonga/fcbbe0f48899f763afd127682645218c to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.SettingsClient;
public class GpsUtils {
private static final String TAG = "GpsUtils";
private Context context;
private SettingsClient mSettingsClient;
private LocationSettingsRequest mLocationSettingsRequest;
private LocationManager locationManager;
public GpsUtils(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mSettingsClient = LocationServices.getSettingsClient(context);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10 * 1000);
locationRequest.setFastestInterval(2 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
mLocationSettingsRequest = builder.build();
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
}
// method for turn on GPS
public void turnGPSOn(onGpsListener onGpsListener) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (onGpsListener != null) {
onGpsListener.gpsStatus(true);
}
} else {
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener((Activity) context, locationSettingsResponse -> {
// GPS is already enable, callback GPS status through listener
if (onGpsListener != null) {
onGpsListener.gpsStatus(true);
}
})
.addOnFailureListener((Activity) context, e -> {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult((Activity) context, Const.GPS_REQUEST);
} catch (IntentSender.SendIntentException sie) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
String errorMessage = "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings.";
Log.e(TAG, errorMessage);
Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show();
}
});
}
}
public interface onGpsListener {
void gpsStatus(boolean isGPSEnable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment