Dynamically add Multiple Runtime Permission

Permission are a thing now. Presently from Marshmallow on words android require Additional runtime permissions. Above all in order to work properly it’s essential. You know what mean. Presumably we specify each permissions multiple times in a class. However this tutorial is about adding multiple permissions dynamically.Yes that’s right.First Place all the required permissions in an array then call a method it’s that simple. As a matter of fact Only devices above Marshmallow have runtime permissions. You must keep in mind that to.
Permission
There are two types of permissions.
- Normal permissions
- Dangerous permissions
1- Normal permission
Presently those permissions the system automatically grants to your app.Permissions Like internet.
2-Dangerous permissions
Similarly those permissions user must agree to grand. Examples are permissions for location ,sms etc…
Download source code for free
Source code
Finally you only need to add which permissions to request in the string array.
String[] permissionList = new String[]{ Manifest.permission.SEND_SMS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA};
PermissionActivity.java
public class PermissionActivity extends AppCompatActivity { String[] permissionList = new String[]{ Manifest.permission.SEND_SMS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA}; private boolean isSettings = false; private static final int PERMISSION_CALLBACK = 111; private static final int PERMISSION_REQUEST = 222; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_permission); if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){ ChkPerm(); }else{ noPermission(); } } private void noPermission() { //devices below Marshmellow Intent intent=new Intent(PermissionActivity.this,MainActivity.class); intent.putExtra("isAbove",false); startActivity(intent); finish(); } private void afterPermission() { Intent intent=new Intent(PermissionActivity.this,MainActivity.class); intent.putExtra("isAbove",true); startActivity(intent); finish(); } public void ChkPerm(){ if(forSelfPermission()){ if(shouldShow()){ permissionCallBack(); } else { //just request the permission ActivityCompat.requestPermissions(PermissionActivity.this,permissionList, PERMISSION_CALLBACK); } } else { //You already have the permission, just go ahead. afterPermission(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode == PERMISSION_CALLBACK){ //check if all permissions are granted boolean allgranted = false; for(int i=0;i<grantResults.length;i++){ if(grantResults[i]==PackageManager.PERMISSION_GRANTED){ allgranted = true; } else { allgranted = false; break; } } if(allgranted){ afterPermission(); } else if(shouldShow()){ permissionCallBack(); } else { permissionSettings(); Toast.makeText(getBaseContext(),"Unable to get Permission", Toast.LENGTH_LONG).show(); } } } private boolean forSelfPermission(){ boolean allgranted = false; for(int i=0;i<permissionList.length;i++){ if (ActivityCompat.checkSelfPermission(PermissionActivity.this, permissionList[i]) != PackageManager.PERMISSION_GRANTED) { allgranted = true; break; } else { allgranted = false; } } if (allgranted){ return true; }else{ return false; } } private boolean resultPermission(){ boolean allgranted = false; for(int i=0;i<permissionList.length;i++){ if (ActivityCompat.checkSelfPermission(PermissionActivity.this, permissionList[i]) == PackageManager.PERMISSION_GRANTED) { allgranted = true; } else { allgranted = false; break; } } if (allgranted){ return true; }else{ return false; } } private boolean shouldShow(){ boolean allgranted = false; for(int i=0;i<permissionList.length;i++){ if (ActivityCompat.shouldShowRequestPermissionRationale(PermissionActivity.this, permissionList[i])) { allgranted = true; break; } else { allgranted = false; } } if (allgranted){ return true; }else{ return false; } } private void permissionCallBack(){ AlertDialog.Builder builder = new AlertDialog.Builder(PermissionActivity.this); builder.setTitle("Need Multiple Permissions"); builder.setMessage("This app needs Multiple permissions."); builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); ActivityCompat.requestPermissions(PermissionActivity.this,permissionList, PERMISSION_CALLBACK); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } private void permissionSettings(){ AlertDialog.Builder builder = new AlertDialog.Builder(PermissionActivity.this); builder.setTitle("Need Multiple Permissions"); builder.setMessage("This app needs permission allow them from settings."); builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); isSettings = true; Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivityForResult(intent, PERMISSION_REQUEST); // Toast.makeText(getBaseContext(), "Go to Permissions to Grant Camera and Location", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d("R","result"); if (requestCode == PERMISSION_REQUEST) { if (resultPermission()){ Log.d("R","result s"); afterPermission(); }else{ Log.d("R","result c"); ChkPerm(); } } } @Override protected void onPostResume() { super.onPostResume(); if (isSettings) { Log.d("R","resume"); if (resultPermission()){ Log.d("R","resume s"); afterPermission(); }else{ Log.d("R","resume c"); ChkPerm(); } isSettings=false; } } }
You can check out Dynamic Action Bar color