Skip to content

Instantly share code, notes, and snippets.

@krunal3kapadiya
Created May 4, 2017 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krunal3kapadiya/ef9683c50e093fa58a482d35d78d8949 to your computer and use it in GitHub Desktop.
Save krunal3kapadiya/ef9683c50e093fa58a482d35d78d8949 to your computer and use it in GitHub Desktop.
Ask multiple runtime permissions
public class MainActivity extends AppCompatActivity {
private final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// list of permissions
String[] permission = new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO};
// checking permissions, if granted or not
if ((ContextCompat.checkSelfPermission(mContext, permission[0])) +
(ContextCompat.checkSelfPermission(mContext, permission[1])) +
(ContextCompat.checkSelfPermission(mContext, permission[2]))
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permission, PERMISSION_REQUEST_CODE);
} else {
callMethodToRunTask();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& (grantResults[0] + grantResults[1] + grantResults[2]) == PackageManager.PERMISSION_GRANTED) {
callMethodToRunTask();
}
}
}
public void callMethodToRunTask() {
// implement code
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment