Last active
December 6, 2022 23:07
-
-
Save mitchtabian/2b9a3dffbfdc565b81f8d26b25d059bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
I got an error
what kind of error?
a question: where is the Constants file of util? i can't find it. My android studio version is 3.5.3
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
I got an error