From Android 6.0 (Marshmallow) API 23 you need to request permission when users install and launch your app. You can also request permission in run time like when your app needs to access the camera you can request permission to access the camera.
If your device is running on the latest version of Android, and you install an application and launch you get this type of popup. In the old version of Android, you have no need to request permission most of the permission is already given. But in the latest Android version, you always need to request permission if you want to do a specific task like read, write, or access a camera.
There are two types of permission in android.
- Install-Time Permissions
- Run-Time Permissions
Install Time permissions in Android
If your device runs on Android 5.1.1 (API 22) or lower and you go to Google play store to install the application you find that the permission is requested at the installation time.
Run Time permission in Android
If the device runs on Android 6 (API 23) or higher, the permission is requested at the run time when you launch your app for the first time. Now if the user allows access to request permission your app able to access that or else a user can deny that permission.
Request Run Time permission in Android
If you want to add permission in Android you can open AndroidManifest.xml file and inside <manifest>...</manifest>
tag you can add permission.
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You can request permission as shown in the above code snippet.
Now in the latest version of Android, you can request for permission using ActivityCompat.requestPermissions
. You can follow the complete step by step implementation below.
Step 1. When the user launches the app you need to check if permission is given or not using the below code.
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){ if(checkSelfPermission(Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){ if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){ /* Go to setting to allow access */ }else{ /* Show popup to allow access*/ } } }
As shown in the above code whenever the user opens the app we check if the user gives permission to access the camera or not and also check for the version above which we need to run this code.
Step 2. Now we can request permission if not given.
ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSION_REQUEST_CODE );
Add the above code to show permission popup. You can add this code in else part of step 1.
Step 3. When the user presses any of button (Allow/deny) we need to handle that response inside onRequestPermissionsResult
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){ if(MY_PERMISSION_REQUEST_CODE == requestCode) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission granted } else { // Permission denied } } }
Step 4. Now if the user denies permission by mistake and your app needs that permission you can add below code and open setting screen from your app from where the user can give you permission.
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setTitle("Permissions Required") .setMessage("You have forcefully denied some of the required permissions " + "for this action. Please open settings, go to permissions and allow them.") .setPositiveButton("Settings", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setCancelable(false) .create() .show();
You can put this code inside step 1 if condition. That’s it, With the help of the above code you can easily request permission on the launch of the app or call above code inside button click.
Complete Code to request run time permission in android App
public class MainActivity extends AppCompatActivity { Button open_camera; final int permission_request = 211; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); open_camera = (Button)findViewById(R.id.open_camera); open_camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Code Inside button if (android.os.Build.VERSION.SDK_INT >= 23) { // only for gingerbread and newer versions String permission = Manifest.permission.CAMERA; if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, permission_request); } else { // Other } } } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case permission_request: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Add your function here which open camera } else { boolean somePermissionsForeverDenied = false; for(String permission: permissions){ if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){ //denied Log.e("denied", permission); }else{ if(ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED){ //allowed Log.e("allowed", permission); } else{ //set to never ask again Log.e("set to never ask again", permission); somePermissionsForeverDenied = true; } } } if(somePermissionsForeverDenied){ final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setTitle("Permissions Required") .setMessage("You have forcefully denied some of the required permissions " + "for this action. Please open settings, go to permissions and allow them.") .setPositiveButton("Settings", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setCancelable(false) .create() .show(); } Toast.makeText(getApplication(), "Permission required", Toast.LENGTH_LONG).show(); } return; } } } }
If this post helps you don’t forget to hit the subscribe button for my youtube channel link above.