Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active December 6, 2022 23:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mitchtabian/2b9a3dffbfdc565b81f8d26b25d059bf to your computer and use it in GitHub Desktop.
Save mitchtabian/2b9a3dffbfdc565b81f8d26b25d059bf to your computer and use it in GitHub Desktop.
private boolean checkMapServices(){
if(isServicesOK()){
if(isMapsEnabled()){
return true;
}
}
return false;
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires GPS to work properly, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
Intent enableGpsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(enableGpsIntent, PERMISSIONS_REQUEST_ENABLE_GPS);
}
});
final AlertDialog alert = builder.create();
alert.show();
}
public boolean isMapsEnabled(){
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
return false;
}
return true;
}
private void getLocationPermission() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
getChatrooms();
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
public boolean isServicesOK(){
Log.d(TAG, "isServicesOK: checking google services version");
int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
if(available == ConnectionResult.SUCCESS){
//everything is fine and the user can make map requests
Log.d(TAG, "isServicesOK: Google Play Services is working");
return true;
}
else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
//an error occured but we can resolve it
Log.d(TAG, "isServicesOK: an error occured but we can fix it");
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOG_REQUEST);
dialog.show();
}else{
Toast.makeText(this, "You can't make map requests", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
mLocationPermissionGranted = false;
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
}
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: called.");
switch (requestCode) {
case PERMISSIONS_REQUEST_ENABLE_GPS: {
if(mLocationPermissionGranted){
getChatrooms();
}
else{
getLocationPermission();
}
}
}
}
@Nahid5702
Copy link

I got an error

@developomp
Copy link

I got an error

what kind of error?

@MomoVCHH
Copy link

MomoVCHH commented Jan 21, 2020

a question: where is the Constants file of util? i can't find it. My android studio version is 3.5.3

@ratiebareeng
Copy link

a question: where is the Constants file of util? i can't find it. My android studio version is 3.5.3

You have to create it yourself. It's just a POJO that you use to declare and initialize your apps' "public static variables" so they're in one place where you can easily find them. Because they're public you can then reference them from anywhere in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment