Skip to content

Instantly share code, notes, and snippets.

@sebastianknopf
Last active October 29, 2018 10:31
Show Gist options
  • Save sebastianknopf/9b76519df003eddd4236e66d43891654 to your computer and use it in GitHub Desktop.
Save sebastianknopf/9b76519df003eddd4236e66d43891654 to your computer and use it in GitHub Desktop.
simple practice for requesting dangerous permissions at runtime in android app
// package ... ;
// import ... ;
/*
* Since Android M has been introduced, apps have the possibility to request critical
* permissions at runtime - the Android Support Library provides several
* methods to achieve this.
* This gist is a simple example activitiy to show how to request the critical
* location permission from a simple android app at runtime.
*/
public class PermissionsActivity extends AppCompatActivity {
private final static int PERMISSION_ACCESS_LOCATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permissions);
this.initComponents();
}
/*
* This method is called every time, when this activiy tries to request a permission
* at runtime. In each permission request you should specify an requestCode which is
* sent back to identify the request again. According to the permission results,
* we load a certain string resource and display the snackbar with this string.
* When the user hits the snackbars action button, the same permission is requested
* once again until the user marks the request as 'Do not ask this again'.
*/
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull int[] grantResults) {
if(grantResults.length < 1 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
int stringResId = 0;
switch(requestCode) {
case PERMISSION_ACCESS_LOCATION:
stringResId = R.string.str_error;
break;
}
Snackbar.make(this.findViewById(R.id.), stringResId, Snackbar.LENGTH_INDEFINITE)
.setActionTextColor(this.getResources().getColor(R.color.colorPrimaryDAY))
.setAction(R.string.str_button, new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(requestCode, permissions[0]);
}
})
.show();
} else {
// YES!! the requested permission has been granted!! restart the initialisation process
this.initComponents();
}
}
/*
* This method is a simple shortcut to ContextCompat.checkSelfPermission. It returns simply
* a true or false depending on the requested permission is already granted.
*/
private boolean checkPermission(String permissionName) {
return ContextCompat.checkSelfPermission(this, permissionName) == PackageManager.PERMISSION_GRANTED;
}
/*
* This method is a shortcut co ActivityCompat.requestPermission. It's only
* a little bit simplified for our needs here, so we don't need to create a
* string array rather than imparting a string containing the permission name.
* The first parameter requestCode is needed for reacting on an permission request
* within the activity.
*/
private void requestPermission(int requestCode, String permissionName) {
ActivityCompat.requestPermissions(this, new String[] {permissionName}, requestCode);
}
/*
* The method initComponents has been extracted from onCreate because maybe we need to call
* from another point again, when some required permissions have been granted.
*/
private void initComponents() {
/*
* This is the point where we can have a look for required permissions. If they're granted
* anyway, that's fine, so we can continue doing our work with initializing
* the application components. But if not, the things we do are also quite simple:
* [0) break the initialisation process]
* 1) We start a single request for the permission to be given
* 2) If the user doesn't grant the requested permission, a snackbar with further information
* will be shown. By clicking the action button of this snackbar, the user is asked again
* to grant the requested permission.
* 3) Until the user marks the request as 'do not ask this again' - or better - he grants the permission,
* he's in the endless loop described in 2).
*/
if(!this.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
// try to request the missing permission the first time, start the loop
// and break the initialisation process until it's called again
this.requestPermission(PERMISSION_ACCESS_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
return;
}
// do initialisation here ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment