
Runtime Permission is introduction of Android 6.0 Marshmallow. Google has changed the way permissions are handled by the app. Runtime permission is users are prompted for some specific permissions at runtime when they become necessary to use. In this prompt to ask user allow or deny.
Your device is working with Android 6.0 Marshmallow (API 23) handle new permission framework. you can ask permission prompt dialog and the two options is allow or deny. you can click on allow to granted permission in you app installation and you can deny to permission is denied and it is not granted and it can not use this installation app.
you can also grant or revoke permission anytime from settings.
Permission Level in Android
Two type of permission level first is normal permission and second is dangerous permission.
Normal Permission : In this permission is very little or zero effect on users privacy or security are categorized as normal permissions. Example of ACCESS_WIFI_STATE etc.
Dangerous Permission : In this Permission is greater effect on users privacy or security are categorized as dangerous permissions. Example of READ_CONTACTS.
Example of Runtime Permission Marshmallow in Android
Declare Permissions in AndroidManifest.xml File.
1 2 3 4 |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera2" /> <uses-permission android:name="android.permission.CAMERA" /> |
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.bhaumik.MainActivity"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Hello World"/> </LinearLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
package com.bhaumik.runtimepermission; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import static android.Manifest.permission.CAMERA; import static android.Manifest.permission.READ_EXTERNAL_STORAGE; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; public class RuntimePermission extends AppCompatActivity{ private static final int PERMISSION_REQUEST_CODE = 200; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!checkPermission()){ requestPermission(); } } private void requestPermission() { ActivityCompat.requestPermissions(RuntimePermission.this, new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA}, PERMISSION_REQUEST_CODE); } private boolean checkPermission() { int result = ContextCompat.checkSelfPermission(RuntimePermission.this, WRITE_EXTERNAL_STORAGE); int result1 = ContextCompat.checkSelfPermission(RuntimePermission.this, READ_EXTERNAL_STORAGE); int result2 = ContextCompat.checkSelfPermission(RuntimePermission.this, CAMERA); return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED && result2 == PackageManager.PERMISSION_GRANTED; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case PERMISSION_REQUEST_CODE : if (grantResults.length > 0){ boolean storages = grantResults[0] == PackageManager.PERMISSION_GRANTED; boolean Cameras = grantResults[0] == PackageManager.PERMISSION_GRANTED; if (storages && Cameras){ Toast.makeText(RuntimePermission.this, "Permission Granted", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(RuntimePermission.this, "Permission Denied", Toast.LENGTH_SHORT).show(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)){ ShowMsgOkCancel("You need to allow access to the permissions", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA}, PERMISSION_REQUEST_CODE); } } }); return; } } } } } } public void ShowMsgOkCancel(String msg, DialogInterface.OnClickListener listener){ new AlertDialog.Builder(RuntimePermission.this) .setMessage(msg) .setPositiveButton("OK", listener) .setNegativeButton("Cancel", null) .create() .show(); } } |

In this Example of Create Runtime Permission in android. i have declared some permission in manifest file and then create some method to check permission is granted or not and display user permission dialog show to user can ask allow or deny permission in android.