Skip to content

Instantly share code, notes, and snippets.

@Mattamorphic
Created October 24, 2020 15:06
Show Gist options
  • Save Mattamorphic/8081be5ce039f5b8f5e87a8fef8c5a4b to your computer and use it in GitHub Desktop.
Save Mattamorphic/8081be5ce039f5b8f5e87a8fef8c5a4b to your computer and use it in GitHub Desktop.
Runtime permissions handling
package com.example.runtime_permissions;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private LocationManager lm;
private String[] requiredPermissions = new String[]{
// Permissions to request - should be added in the manifest
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
private HashMap<String, Toast> toasts = new HashMap<>();
private final int LOCATION_PERMISSIONS_REQUEST_CODE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
toasts.put(
"LocationPermissionsError",
Toast.makeText(this, "Location permissions denied", Toast.LENGTH_SHORT)
);
setupLocationListener();
}
/**
* Check permissions are accepted or request them
* @return boolean
*/
private boolean checkIfHasPermissions() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true;
for (String permission : requiredPermissions)
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED)
return false;
return true;
}
/**
* Request the required permissions for the app
*/
private void requestPermissions() {
// Request code allows this to be identified in onRequestPermissionsResult
ActivityCompat.requestPermissions(this, requiredPermissions, LOCATION_PERMISSIONS_REQUEST_CODE);
}
/**
* Handles permissions results for LOCATION_PERMISSIONS_REQUEST
* @param grantResults int[] Permissions that have been granted
*/
@SuppressLint("LongLogTag")
private void handleLocationPermissionsResult(int[] grantResults) {
boolean granted = true;
if (grantResults.length > 0) for (int result : grantResults)
if (result != PackageManager.PERMISSION_GRANTED) {
granted = false;
break;
}
Log.d("MainActivity::onRequestPermissionsResult", "Permissions granted: " + granted);
if (granted) setupLocationListener();
else toasts.get("LocationPermissionsError").show();
}
/**
* onRequestPermissions Result callback
* @param requestCode code that requested permissions
* @param permissions permissions that were requested
* @param grantResults results of permissions requested
*/
@SuppressLint("LongLogTag")
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case LOCATION_PERMISSIONS_REQUEST_CODE:
handleLocationPermissionsResult(grantResults);
break;
}
}
/**
* Setup location listener
*/
@SuppressLint({"MissingPermission", "LongLogTag"})
private void setupLocationListener() {
// If >= APIv23 we need to check the runtime permissions
if (!checkIfHasPermissions()) {
requestPermissions();
return;
}
Log.d("MainActivity::setupLocationListener", "Perms OK, configuring location listener");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("MainActivity::setupLocationListener", "Location changed");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("MainActivity::setupLocationListener", "Provider enabled");
if (provider != LocationManager.GPS_PROVIDER) return;
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("MainActivity::setupLocationListener", "Provider enabled, location:" + location);
}
@Override
public void onProviderDisabled(String provider) {
Log.d("MainActivity::setupLocationListener", "Provider disabled");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment