Site icon C1CTech

Android Working with Runtime Permission

<p>Every Android application is started in its own process thus are isolated from all other applications &lpar;even from system&sol;default applications&rpar;&period; As a result&comma; an Android application can’t access any file or data outside its scope until and unless the file or data is shared with the application&period;<&sol;p>&NewLine;<p>So&comma; the conclusion is that if an application needs anything outside its scope&comma; then it has to request the appropriate permission<i>&period;<&sol;i><&sol;p>&NewLine;<p><em><strong>Get <span style&equals;"color&colon; &num;008000&semi;">Github<&sol;span> code from<&sol;strong> <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;RuntimePermissionDemo">HERE<&sol;a><&sol;strong>&period;<&sol;span> <&sol;em><&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"hgJnQYIg&lowbar;eM" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;hgJnQYIg&lowbar;eM" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;hgJnQYIg&lowbar;eM&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;hgJnQYIg&lowbar;eM&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Android Runtime Permissions<&sol;span><&sol;strong><&sol;h3>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>Runtime permissions<&sol;strong><&sol;span> are a new feature in Android 6&period;0 Marshmallow that allow you to request permissions at runtime&comma; when it matters rather than at install time&period;<&sol;li>&NewLine;<li>This will give the user to have more control over the permissions and ensure more security for his&sol;her confidential data&period;<&sol;li>&NewLine;<li>In the case we try to call some function that requires a permission which user has not granted yet&comma; the function will suddenly throw an Exception <strong><span style&equals;"color&colon; &num;0000ff&semi;">&lpar;java&period;lang&period;SecurityException&rpar;<&sol;span><&sol;strong> that will lead to the application crashing&period; Hence we need to implement this new android permissions model in our application&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Permission Types<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">Permissions are divided into several protection levels&period; The two most important protection levels are normal and dangerous permissions&period;<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Normal Permissions<&sol;b><&sol;span><&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;000080&semi;"><b>Dangerous Permissions<&sol;b><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1"><b><span style&equals;"color&colon; &num;0000ff&semi;">Normal Permissions<&sol;span>&colon;<&sol;b>  Permissions that have very little or no effect on user&&num;8217&semi;s privacy or confidential data are categorized as normal permissions&period; The system itself grants normal permissions when requested instead of prompting to the user&period; Examples would be <span style&equals;"color&colon; &num;008000&semi;"><b>ACCESS&lowbar;WIFI&lowbar;STATE<&sol;b>&comma; <b>WAKE&lowbar;LOCK&comma;<&sol;b> <&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">INTERNET<&sol;span>&comma; <&sol;strong>etc&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Dangerous Permissions<&sol;span><&sol;strong><b>&colon;<&sol;b>  Permissions that may have a greater effect on user&&num;8217&semi;s privacy or confidential data are categorized as dangerous permissions&period; Examples would be  <b><span style&equals;"color&colon; &num;008000&semi;">READ&lowbar;CONTACTS&comma; ACCESS&lowbar;FINE&lowbar;LOCATION&comma; CAMERA<&sol;span>&comma; <&sol;b>etc&period; If an app requests dangerous permissions then the user has to explicitly grant permission to the app&period;<&sol;p>&NewLine;<p class&equals;"p1">Here is the detailed <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;guide&sol;topics&sol;security&sol;permissions&period;html&num;normal-dangerous"><span class&equals;"s1">information<&sol;span><&sol;a><&sol;strong><&sol;span> about permissions&period;<&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Permission Workflow before and from M &lpar;API 23&rpar;<&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">The permission workflow has been changed from Android Marshmallow&lpar;API 23&rpar;&period; The way Android asks the user to grant dangerous permissions depends on the version of Android running on the user&&num;8217&semi;s device&comma; and the system version targeted by your app&period;<&sol;p>&NewLine;<p><b> <span style&equals;"color&colon; &num;0000ff&semi;">Permission Model before M &lpar;API 23&rpar;&colon;<&sol;span><&sol;b><span style&equals;"color&colon; &num;0000ff&semi;"> <&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li>Before API 23&comma; the permission model was simpler to the developer but offered less control and security to the user&period;<&sol;li>&NewLine;<li>If the device is running Android 5&period;1&period;1 &lpar;API level 22&rpar; or lower&comma; <em>or<&sol;em> the app&&num;8217&semi;s <strong><span style&equals;"color&colon; &num;008000&semi;">targetSdkVersion<&sol;span><&sol;strong> is 22 or lower while running on any version of Android&comma; the system automatically asks the user to grant all dangerous permissions for your app at install-time&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<figure id&equals;"attachment&lowbar;1261" aria-describedby&equals;"caption-attachment-1261" style&equals;"width&colon; 323px" class&equals;"wp-caption aligncenter"><img class&equals;"alignnone size-full wp-image-1261" src&equals;"https&colon;&sol;&sol;i2&period;wp&period;com&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot-2019-10-22-19&period;58&period;41-2154046784-1571817449429&period;png&quest;ssl&equals;1&amp&semi;w&equals;646" alt&equals;"Screenshot 2019-10-22 19&period;58&period;41" width&equals;"323" height&equals;"574" &sol;><figcaption id&equals;"caption-attachment-1261" class&equals;"wp-caption-text">Install-time permission dialog<&sol;figcaption><&sol;figure>&NewLine;<ul>&NewLine;<li>If the user clicks <span style&equals;"color&colon; &num;008000&semi;"><strong>Accept<&sol;strong><&sol;span>&comma; all permissions the app requests are granted&period; If the user denies the permissions request&comma; the system cancels the installation of the app&period;<&sol;li>&NewLine;<li>Permissions cannot be denied or granted after the installation&period;<&sol;li>&NewLine;<li>Thus developers were required only to declare all needed permissions in the manifest and then just forget&comma; if the app is running then it has all the requested permissions&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b><span style&equals;"color&colon; &num;0000ff&semi;">Permission Model from M &lpar;API 23&rpar;&colon;<&sol;span><&sol;b><span style&equals;"color&colon; &num;0000ff&semi;"> <&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li>With Android 6&period;0 Marshmallow &lpar;API 23&rpar;&comma; a new runtime permission model has been introduced&period;<&sol;li>&NewLine;<li>According to this model if the device is running Android 6&period;0 &lpar;API level 23&rpar; or higher&comma; and the app&&num;8217&semi;s <strong><span style&equals;"color&colon; &num;008000&semi;">targetSdkVersion<&sol;span><&sol;strong> is 23 or higher&comma; the user isn&&num;8217&semi;t notified of any app permissions at install time&period;<&sol;li>&NewLine;<li>Your app must ask the user to grant the dangerous permissions at runtime before performing any action that may require the particular permission&period;<&sol;li>&NewLine;<li>When your app requests permission&comma; the user sees a system dialog which includes a <span style&equals;"color&colon; &num;008000&semi;"><b>Deny<&sol;b><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><b>Allow<&sol;b><&sol;span> button&period;<&sol;li>&NewLine;<li>If the user denies the permission request&comma; the next time your app requests the permission&comma; the dialog contains a checkbox that&comma; when checked&comma; indicates the user doesn&&num;8217&semi;t want to be prompted for the permission again&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"alignnone wp-image-1251" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571749993&period;png" alt&equals;"Screenshot&lowbar;1571749993" width&equals;"316" height&equals;"562" &sol;>        <img class&equals;"alignnone wp-image-1254" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571750449&period;png" alt&equals;"Screenshot&lowbar;1571750449" width&equals;"315" height&equals;"560" &sol;><&sol;p>&NewLine;<p class&equals;"p2"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Note&colon;<&sol;b> <&sol;span>According to Google&comma; beginning with Android 6&period;0 &lpar;API level 23&rpar;&comma; users can revoke permissions from any app at any time&comma; even if the app targets a lower API level&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Check for permission<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>If your app needs a dangerous permission&comma; then you must check whether you have that permission or not every time when the app performs an operation that requires that permission&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example<&sol;span><&sol;strong>&comma; if your app uses the camera feature&comma; then you have to check for the camera permission every time when app opens the camera&period;<&sol;p>&NewLine;<p>To check if you have a permission&comma; call the <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"pln">checkSelfPermission&lpar;&rpar;<&sol;span><&sol;strong><&sol;span> method&period; For example&comma; this snippet shows how to check if the activity has permission to access the camera and location&colon;<&sol;p>&NewLine;<pre>private boolean checkPermission&lpar;&rpar; &lbrace;&NewLine;&NewLine;<span class&equals;"typ"><strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;Assume thisActivity as current activity<&sol;span><&sol;strong><&sol;span>&NewLine; int result1 &equals; ContextCompat&period;checkSelfPermission&lpar;<span class&equals;"pln">thisActivity<&sol;span>&comma; <span class&equals;"typ">Manifest<&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"pln">permission<&sol;span><span class&equals;"pun">&period;<&sol;span>ACCESS&lowbar;FINE&lowbar;LOCATION&rpar;&semi;&NewLine; int result2 &equals; ContextCompat&period;checkSelfPermission&lpar;<span class&equals;"pln">thisActivity<&sol;span>&comma; <span class&equals;"typ">Manifest<&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"pln">permission<&sol;span><span class&equals;"pun">&period;<&sol;span>CAMERA&rpar;&semi;&NewLine;&NewLine; return result1 &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED &amp&semi;&amp&semi; result2 &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine;&rcub;<&sol;pre>&NewLine;<p>If the app has permission&comma; then the method returns <strong><span style&equals;"color&colon; &num;008000&semi;">packageManager&period;PERMISSION&lowbar;GRANTED<&sol;span><&sol;strong> &lpar;ie&period; 0&rpar; and the app can continue with the operation&period;<&sol;p>&NewLine;<p>If the app does not have permission&comma; then the method returns <strong><span style&equals;"color&colon; &num;008000&semi;">packageManager&period;PERMISSION&lowbar;DENIED<&sol;span> <&sol;strong>&lpar;ie&period; -1&rpar; and the app have to explicitly ask the user for permission&period;<&sol;p>&NewLine;<h3 id&equals;"explain"><strong><span style&equals;"color&colon; &num;000080&semi;">Explain why the app needs permissions<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>In some circumstances&comma; you want to help the user understand why your app needs a permission&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example<&sol;span><&sol;strong>&comma; if you write a camera app&comma; requesting the camera permission would be expected by the user and no rationale for why it is requested is needed&period; If however&comma; the app needs location for tagging photos then a non-tech savvy user may wonder how location is related to taking photos&period; In this case&comma; you may choose to show UI with rationale of requesting this permission&period;<&sol;p>&NewLine;<p>Android provides a utility method&comma; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>shouldShowRequestPermissionRationale&lpar;&rpar;<&sol;strong><&sol;span>&comma; that returns <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span> if the user has previously denied the request&comma; and returns <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong> if a user has denied a permission and selected the <span style&equals;"color&colon; &num;008000&semi;"><strong>Don&&num;8217&semi;t ask again<&sol;strong><&sol;span> option in the permission request dialog&comma; or if a device policy prohibits the permission&period;<&sol;p>&NewLine;<pre>if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;shouldShowRequestPermissionRationale&lpar;ACCESS&lowbar;FINE&lowbar;LOCATION&rpar; &vert;&vert; <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">shouldShowRequestPermissionRationale&lpar;CAMERA&rpar; returns true if &NewLine;the user has previously denied any of the request<&sol;span><&sol;strong>&NewLine; if &lpar;shouldShowRequestPermissionRationale&lpar;ACCESS&lowbar;FINE&lowbar;LOCATION&rpar; &vert;&vert; shouldShowRequestPermissionRationale&lpar;CAMERA&rpar;&rpar; &lbrace;&NewLine;&NewLine; showMessageGrantCancel&lpar;"You need to allow access to both the permissions"&comma;&NewLine; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;DialogInterface dialog&comma; int which&rpar; &lbrace;&NewLine; &NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;when the user click on grant button then &NewLine;requestPermission&lpar;&rpar; will call requesting array of permissions&period; <&sol;span> <&sol;strong>&NewLine;if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;&NewLine; requestPermissions&lpar;new String&lbrack;&rsqb;&lbrace; ACCESS&lowbar;FINE&lowbar;LOCATION&comma; ACCESS&lowbar;COARSE&lowbar;LOCATION&comma; CAMERA&rcub;&comma; PERMISSION&lowbar;REQUEST&lowbar;CODE&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub; &NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;showMessageGrantCancel&lpar;&rpar; will show an alert dialog with rationale of <&sol;span><&sol;strong>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>requesting this permissions<&sol;strong><&sol;span>&NewLine;private void showMessageGrantCancel&lpar;String message&comma; DialogInterface&period;OnClickListener okListener&rpar; &lbrace;&NewLine; new AlertDialog&period;Builder&lpar;MainActivity&period;this&rpar;&NewLine; &period;setMessage&lpar;message&rpar;&NewLine; &period;setPositiveButton&lpar;"GRANT"&comma; okListener&rpar;&NewLine; &period;setNegativeButton&lpar;"CANCEL"&comma; null&rpar;&NewLine; &period;create&lpar;&rpar;&NewLine; &period;show&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Request and Handle Runtime permission<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>When your app receives <strong><span style&equals;"color&colon; &num;008000&semi;">PERMISSION&lowbar;DENIED<&sol;span><&sol;strong> from <strong><span style&equals;"color&colon; &num;0000ff&semi;">checkSelfPermission&lpar;&rpar;<&sol;span><&sol;strong>&comma; you need to prompt the user for that permission&period;  To request the permissions Android provides a method called <strong><span style&equals;"color&colon; &num;0000ff&semi;">requestPermissions&lpar;&rpar;<&sol;span><&sol;strong> to request array of permissions to be granted to the application and these permissions must be requested in your manifest&period; Calling these methods brings up a standard Android dialog&comma; which you cannot customize&period; When the user responds to your app&&num;8217&semi;s permission request&comma; the system invokes your app&&num;8217&semi;s <strong><span style&equals;"color&colon; &num;0000ff&semi;">onRequestPermissionsResult&lpar;&rpar;<&sol;span><&sol;strong> method&comma; passing it the user response&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Request runtime permission<&sol;span><&sol;strong><&sol;h4>&NewLine;<ul>&NewLine;<li>If your app doesn&&num;8217&semi;t already have the permission it needs&comma; the app must call one of the <strong><span style&equals;"color&colon; &num;0000ff&semi;">requestPermissions&lpar;&rpar;<&sol;span><&sol;strong> methods to request the appropriate permissions&period;<&sol;li>&NewLine;<li>Your app passes the permissions it wants and an integer <span style&equals;"color&colon; &num;008000&semi;"><strong>request code<&sol;strong><&sol;span> that you specify to identify this permission request&period;<&sol;li>&NewLine;<li>This method functions asynchronously&period; It returns right away&comma; and after the user responds to the prompt&period;<&sol;li>&NewLine;<li>The system calls the app&&num;8217&semi;s callback method <span style&equals;"color&colon; &num;0000ff&semi;"><strong>onRequestPermissionsResult&lpar;&rpar;<&sol;strong><&sol;span> with the results&comma; passing the same request code that the app passed to <strong><span style&equals;"color&colon; &num;0000ff&semi;">requestPermissions&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>The following code checks if the app has permission to access <strong><span style&equals;"color&colon; &num;008000&semi;">CAMERA<&sol;span><&sol;strong>&period; If it does not have permission it checks if it should show an explanation for needing the permission&comma; and if no explanation is needed&comma; it requests the permission&colon;<&sol;p>&NewLine;<pre><strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; Here&comma; thisActivity is the current activity<&sol;span><&sol;strong>&NewLine;if &lpar;ContextCompat&period;checkSelfPermission&lpar;thisActivity&comma;&NewLine; Manifest&period;permission&period;CAMERA&rpar;&NewLine; &excl;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&rpar; &lbrace;&NewLine;&NewLine; &sol;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol; Permission is not granted<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol; Should we show an explanation&quest;<&sol;span><&sol;strong>&NewLine; if &lpar;ActivityCompat&period;shouldShowRequestPermissionRationale&lpar;thisActivity&comma;&NewLine; Manifest&period;permission&period;CAMERA&rpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; Show an explanation to the user &ast;asynchronously&ast; -- don't block<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol; this thread waiting for the user's response&excl; After the user<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol; sees the explanation&comma; try again to request the permission&period;<&sol;span><&sol;strong>&NewLine; &rcub; else &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; No explanation needed&semi; request the permission<&sol;span><&sol;strong>&NewLine; ActivityCompat&period;requestPermissions&lpar;thisActivity&comma;&NewLine; new String&lbrack;&rsqb;&lbrace; Manifest&period;permission&period;CAMERA&rcub;&comma;PERMISSION&lowbar;REQUEST&lowbar;CODE&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;PERMISSION&lowbar;REQUEST&lowbar;CODE is an<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; app-defined int constant&period; The callback method gets the<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; result of the request&period;<&sol;span><&sol;strong>&NewLine;&rcub;&NewLine;&rcub; else &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; Permission has already been granted<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<h4 id&equals;"handle-response"><span style&equals;"color&colon; &num;000080&semi;"><strong>Handle the permissions request response<&sol;strong><&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li>When the user responds to your app&&num;8217&semi;s permission request&comma; the system invokes your app&&num;8217&semi;s <strong><span style&equals;"color&colon; &num;0000ff&semi;">onRequestPermissionsResult&lpar;&rpar;<&sol;span><&sol;strong> method&comma; passing it the user response&period; Your app has to override that method to find out whether the permission was granted&period; The callback is passed the same request code you passed to <strong><span style&equals;"color&colon; &num;0000ff&semi;">requestPermissions&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>For example&comma; if an app requests <span style&equals;"color&colon; &num;008000&semi;"><strong>CAMERA<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>LOCATION<&sol;strong><&sol;span> access it might have the following callback method&colon;<&sol;p>&NewLine;<pre>&commat;Override&NewLine;public void onRequestPermissionsResult&lpar;int requestCode&comma; String permissions&lbrack;&rsqb;&comma; int&lbrack;&rsqb; grantResults&rpar; &lbrace;&NewLine; switch &lpar;requestCode&rpar; &lbrace;&NewLine; case PERMISSION&lowbar;REQUEST&lowbar;CODE&colon;&NewLine;&NewLine;<span class&equals;"com"><strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; If request is cancelled&comma; the result arrays are empty&period;<&sol;span><&sol;strong>&NewLine;<&sol;span>if &lpar;grantResults&period;length &gt&semi; 0&rpar; &lbrace;&NewLine;&NewLine;boolean AcceptedFineLocation &equals; grantResults&lbrack;0&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine;boolean AcceptedCamera &equals; grantResults&lbrack;1&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine;&NewLine;if &lpar;AcceptedFineLocation &amp&semi;&amp&semi; AcceptedCamera&rpar;&NewLine;Toast&period;makeText&lpar;this&comma; "Permission Granted&comma; Now you can access location data and camera&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;else &lbrace;&NewLine;Toast&period;makeText&lpar;this&comma; "Permission Denied&comma; You cannot access location data and camera&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&NewLine;break&semi;&NewLine;&rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<ul>&NewLine;<li>If the user denies a permission request&comma; your app should take appropriate action&period; For example&comma; your app might show a dialog explaining why it could not perform the user&&num;8217&semi;s requested action that needs that permission&period;<&sol;li>&NewLine;<li>When the system asks the user to grant a permission&comma; the user has the option of telling the system not to ask for that permission again&period; In that case&comma; any time an app uses <span style&equals;"color&colon; &num;0000ff&semi;"><strong>requestPermissions&lpar;&rpar;<&sol;strong><&sol;span> to ask for that permission again&comma; the system immediately denies the request&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Understand the workflow of runtime permission request<&sol;span><&sol;strong><&sol;h3>&NewLine;<ul>&NewLine;<li>When the app asks for dangerous permission for the first time&comma; then the system shows a standard non-customizable dialog that specifies the permission features&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"alignnone wp-image-1250" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571749987-576x1024&period;png" alt&equals;"Screenshot&lowbar;1571749987" width&equals;"303" height&equals;"539" &sol;>     <img class&equals;"alignnone wp-image-1251" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571749993&period;png" alt&equals;"Screenshot&lowbar;1571749993" width&equals;"301" height&equals;"535" &sol;><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul>&NewLine;<li>If the user denies permission and the app tries to access the same permission again then it is a good programming practice to show an explanation to the user that why the app needs that permission&period; This is not a system dialog but showing explanation is the responsibility of the user&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;" wp-image-1253 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571750324&period;png" alt&equals;"Screenshot&lowbar;1571750324" width&equals;"321" height&equals;"571" &sol;><&sol;p>&NewLine;<ul>&NewLine;<li>If a previously denied permission request again&comma; then the system shows a standard dialog with an additional <strong><span style&equals;"color&colon; &num;008000&semi;">checkbox &lpar;Don&&num;8217&semi;t ask again&rpar;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li>If the user selects the checkbox&lpar;Don&&num;8217&semi;t ask again&rpar; then the system will never show that permission dialog again&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;" wp-image-1257 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571750895&period;png" alt&equals;"Screenshot&lowbar;1571750895" width&equals;"319" height&equals;"567" &sol;><&sol;p>&NewLine;<ul>&NewLine;<li>If the user selects the checkbox&lpar;Don&&num;8217&semi;t ask again&rpar;  and denies the permission for the second time then it is a good programming practice to lead the user to the permission page in app settings to grant the denied permissions&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"alignnone wp-image-1255" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571750569&period;png" alt&equals;"Screenshot&lowbar;1571750569" width&equals;"320" height&equals;"569" &sol;>        <img class&equals;"alignnone wp-image-1256" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571750591&period;png" alt&equals;"Screenshot&lowbar;1571750591" width&equals;"320" height&equals;"569" &sol;><&sol;p>&NewLine;<p>So now let us start with creating a sample app&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating Android Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; Create a new project in Android Studio from <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Project<&sol;strong> <&sol;span>and fill the project details&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Open <strong>build&period;gradle<&sol;strong> &lpar;app level&rpar; and make sure that you have set <span style&equals;"color&colon; &num;008000&semi;"><strong>minSdkVersion<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>targetSdkVersion<&sol;strong><&sol;span> as we have to support the permissions model in lower versions also&period; I am keeping minSdkVersion to <strong>17<&sol;strong> and targetSdkVersion to <strong>28<&sol;strong>&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;<div>&NewLine;<pre>apply plugin&colon; 'com&period;android&period;application'&NewLine;&NewLine;android &lbrace;&NewLine; compileSdkVersion 28&NewLine; defaultConfig &lbrace;&NewLine; &period;&period;&period;&period;&NewLine;&NewLine; minSdkVersion 17&NewLine; targetSdkVersion 28&NewLine;&NewLine; &period;&period;&period;&period;&NewLine; &NewLine; &rcub;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><strong>3<&sol;strong>&period; Though we will request the permissions at runtime&comma; we should add it to the Manifest also&period; We will start with the <strong><span style&equals;"color&colon; &num;008000&semi;">CAMERA<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;008000&semi;"><strong>ACCESS&lowbar;FINE&lowbar;LOCATION<&sol;strong><&sol;span> permissions&period; Add these permissions to your <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> just before the application tag&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&excl;--&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;--><&excl;--&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;--><&excl;--&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;--><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;manifest xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; package&equals;"trendlife&period;myapplication"&gt&semi;&NewLine; &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;ACCESS&lowbar;FINE&lowbar;LOCATION"&sol;&gt&semi;&NewLine; &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;CAMERA"&sol;&gt&semi;&NewLine; &NewLine;&NewLine; &lt&semi;application&NewLine; android&colon;allowBackup&equals;"true"&NewLine; android&colon;icon&equals;"&commat;mipmap&sol;ic&lowbar;launcher"&NewLine; android&colon;label&equals;"&commat;string&sol;app&lowbar;name"&NewLine; android&colon;roundIcon&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round"&NewLine; android&colon;supportsRtl&equals;"true"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme"&gt&semi;&NewLine; &lt&semi;activity android&colon;name&equals;"&period;MainActivity"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine; &lt&semi;&sol;activity&gt&semi;&NewLine; &lt&semi;&sol;application&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;manifest&gt&semi;<&sol;pre>&NewLine;<p><strong>4<&sol;strong>&period; Open the layout file of main activity &lpar;<strong>activity&lowbar;main&period;xml<&sol;strong>&rpar; and add the below xml&period; This layout contains two buttons ie&period; <strong><span style&equals;"color&colon; &num;008000&semi;">CHECK PERMISSION<&sol;span><&sol;strong> to check the permission and <span style&equals;"color&colon; &num;008000&semi;"><strong>REQUEST PERMISSION<&sol;strong><&sol;span> to request the permissions&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;orientation&equals;"vertical"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;check&lowbar;permission"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;color&sol;colorAccent"&NewLine; android&colon;padding&equals;"8dp"&NewLine; android&colon;text&equals;"Check Permission" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;request&lowbar;permission"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"10dp"&NewLine; android&colon;background&equals;"&commat;color&sol;colorAccent"&NewLine; android&colon;padding&equals;"8dp"&NewLine; android&colon;text&equals;"Request Permission" &sol;&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">The above layout generates a screen something like this&period;<&sol;span><&sol;p>&NewLine;<div><&sol;div>&NewLine;<div><img class&equals;" wp-image-1259 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1571751672&period;png" alt&equals;"Screenshot&lowbar;1571751672" width&equals;"366" height&equals;"651" &sol;><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><strong>5<&sol;strong>&period; Now open <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and write the below code on <span style&equals;"color&colon; &num;008000&semi;"><strong>CHECK PERMISSION<&sol;strong><&sol;span> button click &colon;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<pre>public void onClick&lpar;View v&rpar; &lbrace;&NewLine;&NewLine;&NewLine; int id &equals; v&period;getId&lpar;&rpar;&semi;&NewLine; switch &lpar;id&rpar; &lbrace;&NewLine; case R&period;id&period;check&lowbar;permission&colon;&NewLine; if &lpar;checkPermission&lpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine; Toast&period;makeText&lpar;this&comma; "Permission already granted&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub; else &lbrace;&NewLine; Toast&period;makeText&lpar;this&comma; "Please request permission&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;private boolean checkPermission&lpar;&rpar; &lbrace;&NewLine; int result1 &equals; ContextCompat&period;checkSelfPermission&lpar;getApplicationContext&lpar;&rpar;&comma; ACCESS&lowbar;FINE&lowbar;LOCATION&rpar;&semi;&NewLine; int result2 &equals; ContextCompat&period;checkSelfPermission&lpar;getApplicationContext&lpar;&rpar;&comma; CAMERA&rpar;&semi;&NewLine;&NewLine; return result1 &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED &amp&semi;&amp&semi; result2 &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine;&rcub;<&sol;pre>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>6&period; In <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> write the below code on <span style&equals;"color&colon; &num;008000&semi;"><strong>REQUEST PERMISSION<&sol;strong><&sol;span> button click &colon;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<pre>public class MainActivity extends AppCompatActivity implements View&period;OnClickListener &lbrace;&NewLine;&NewLine;private static final int PERMISSION&lowbar;REQUEST&lowbar;CODE &equals; 200&semi;&NewLine;&NewLine;&commat;Override&NewLine;public void onClick&lpar;View v&rpar; &lbrace;&NewLine;&NewLine;&NewLine;int id &equals; v&period;getId&lpar;&rpar;&semi;&NewLine;switch &lpar;id&rpar; &lbrace;&NewLine;&NewLine;case R&period;id&period;request&lowbar;permission&colon;&NewLine;if &lpar;&excl;checkPermission&lpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine;requestPermission&lpar;&rpar;&semi;&NewLine;&NewLine;&rcub; else &lbrace;&NewLine;Toast&period;makeText&lpar;this&comma; "Permission already granted&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine;&rcub;&NewLine;break&semi;&NewLine;&rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;&NewLine;private void requestPermission&lpar;&rpar; &lbrace;&NewLine;&NewLine;ActivityCompat&period;requestPermissions&lpar;this&comma; new String&lbrack;&rsqb;&lbrace;ACCESS&lowbar;FINE&lowbar;LOCATION&comma; CAMERA&rcub;&comma; PERMISSION&lowbar;REQUEST&lowbar;CODE&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>7&period; When the user responds to your app&&num;8217&semi;s permission request&comma; the system invokes your<&sol;div>&NewLine;<div>app&&num;8217&semi;s <strong><span style&equals;"color&colon; &num;0000ff&semi;">onRequestPermissionsResult&lpar;&rpar;<&sol;span><&sol;strong> method corresponding to the request code&comma; and passing it the user response&period;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<pre>&commat;Override&NewLine;public void onRequestPermissionsResult&lpar;int requestCode&comma; String permissions&lbrack;&rsqb;&comma; int&lbrack;&rsqb; grantResults&rpar; &lbrace;&NewLine; switch &lpar;requestCode&rpar; &lbrace;&NewLine; case PERMISSION&lowbar;REQUEST&lowbar;CODE&colon;&NewLine; if &lpar;grantResults&period;length &gt&semi; 0&rpar; &lbrace;&NewLine;&NewLine; boolean AcceptedFineLocation &equals; grantResults&lbrack;0&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine; boolean AcceptedCamera &equals; grantResults&lbrack;1&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine;&NewLine; if &lpar;AcceptedFineLocation &amp&semi;&amp&semi; AcceptedCamera&rpar;&NewLine; Toast&period;makeText&lpar;this&comma; "Permission Granted&comma; Now you can access location data and camera&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; else &lbrace;&NewLine; Toast&period;makeText&lpar;this&comma; "Permission Denied&comma; You cannot access location data and camera&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;&NewLine; if &lpar;shouldShowRequestPermissionRationale&lpar;ACCESS&lowbar;FINE&lowbar;LOCATION&rpar; &vert;&vert; shouldShowRequestPermissionRationale&lpar;CAMERA&rpar;&rpar; &lbrace;&NewLine;&NewLine; showMessageGrantCancel&lpar;"You need to allow access to both the permissions"&comma;&NewLine; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;DialogInterface dialog&comma; int which&rpar; &lbrace;&NewLine; if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;&NewLine; requestPermissions&lpar;new String&lbrack;&rsqb;&lbrace;ACCESS&lowbar;FINE&lowbar;LOCATION&comma; ACCESS&lowbar;COARSE&lowbar;LOCATION&comma; CAMERA&rcub;&comma; PERMISSION&lowbar;REQUEST&lowbar;CODE&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; return&semi;&NewLine; &rcub; else &lbrace;&NewLine; openSettingsDialog&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; break&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>8&period; Below is the complete code of <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;java<&sol;span>&period;<&sol;strong><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<pre>package trendlife&period;myapplication&semi;&NewLine;&NewLine;import android&period;content&period;DialogInterface&semi;&NewLine;import android&period;content&period;Intent&semi;&NewLine;import android&period;content&period;pm&period;PackageManager&semi;&NewLine;import android&period;net&period;Uri&semi;&NewLine;import android&period;os&period;Build&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;provider&period;Settings&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AlertDialog&semi;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;import androidx&period;core&period;app&period;ActivityCompat&semi;&NewLine;import androidx&period;core&period;content&period;ContextCompat&semi;&NewLine;&NewLine;import static android&period;Manifest&period;permission&period;ACCESS&lowbar;COARSE&lowbar;LOCATION&semi;&NewLine;import static android&period;Manifest&period;permission&period;ACCESS&lowbar;FINE&lowbar;LOCATION&semi;&NewLine;import static android&period;Manifest&period;permission&period;CAMERA&semi;&NewLine;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity implements View&period;OnClickListener &lbrace;&NewLine;&NewLine; private static final int PERMISSION&lowbar;REQUEST&lowbar;CODE &equals; 200&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;&NewLine; Button check&lowbar;permission &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;check&lowbar;permission&rpar;&semi;&NewLine; Button request&lowbar;permission &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;request&lowbar;permission&rpar;&semi;&NewLine; check&lowbar;permission&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; request&lowbar;permission&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine;&NewLine;&NewLine; int id &equals; v&period;getId&lpar;&rpar;&semi;&NewLine; switch &lpar;id&rpar; &lbrace;&NewLine; case R&period;id&period;check&lowbar;permission&colon;&NewLine; if &lpar;checkPermission&lpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine; Toast&period;makeText&lpar;this&comma; "Permission already granted&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub; else &lbrace;&NewLine; Toast&period;makeText&lpar;this&comma; "Please request permission&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; break&semi;&NewLine; case R&period;id&period;request&lowbar;permission&colon;&NewLine; if &lpar;&excl;checkPermission&lpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine; requestPermission&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub; else &lbrace;&NewLine; Toast&period;makeText&lpar;this&comma; "Permission already granted&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; private boolean checkPermission&lpar;&rpar; &lbrace;&NewLine; int result1 &equals; ContextCompat&period;checkSelfPermission&lpar;getApplicationContext&lpar;&rpar;&comma; ACCESS&lowbar;FINE&lowbar;LOCATION&rpar;&semi;&NewLine; int result2 &equals; ContextCompat&period;checkSelfPermission&lpar;getApplicationContext&lpar;&rpar;&comma; CAMERA&rpar;&semi;&NewLine;&NewLine; return result1 &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED &amp&semi;&amp&semi; result2 &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; private void requestPermission&lpar;&rpar; &lbrace;&NewLine;&NewLine; ActivityCompat&period;requestPermissions&lpar;this&comma; new String&lbrack;&rsqb;&lbrace;ACCESS&lowbar;FINE&lowbar;LOCATION&comma; CAMERA&rcub;&comma; PERMISSION&lowbar;REQUEST&lowbar;CODE&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onRequestPermissionsResult&lpar;int requestCode&comma; String permissions&lbrack;&rsqb;&comma; int&lbrack;&rsqb; grantResults&rpar; &lbrace;&NewLine; switch &lpar;requestCode&rpar; &lbrace;&NewLine; case PERMISSION&lowbar;REQUEST&lowbar;CODE&colon;&NewLine; if &lpar;grantResults&period;length &gt&semi; 0&rpar; &lbrace;&NewLine;&NewLine; boolean AcceptedFineLocation &equals; grantResults&lbrack;0&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine; boolean AcceptedCamera &equals; grantResults&lbrack;1&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine;&NewLine; if &lpar;AcceptedFineLocation &amp&semi;&amp&semi; AcceptedCamera&rpar;&NewLine; Toast&period;makeText&lpar;this&comma; "Permission Granted&comma; Now you can access location data and camera&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; else &lbrace;&NewLine; Toast&period;makeText&lpar;this&comma; "Permission Denied&comma; You cannot access location data and camera&period;"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;&NewLine; if &lpar;shouldShowRequestPermissionRationale&lpar;ACCESS&lowbar;FINE&lowbar;LOCATION&rpar; &vert;&vert; shouldShowRequestPermissionRationale&lpar;CAMERA&rpar;&rpar; &lbrace;&NewLine;&NewLine; showMessageGrantCancel&lpar;"You need to allow access to both the permissions"&comma;&NewLine; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;DialogInterface dialog&comma; int which&rpar; &lbrace;&NewLine; if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;&NewLine; requestPermissions&lpar;new String&lbrack;&rsqb;&lbrace;ACCESS&lowbar;FINE&lowbar;LOCATION&comma; ACCESS&lowbar;COARSE&lowbar;LOCATION&comma; CAMERA&rcub;&comma; PERMISSION&lowbar;REQUEST&lowbar;CODE&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; return&semi;&NewLine; &rcub; else &lbrace;&NewLine; openSettingsDialog&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; break&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; private void showMessageGrantCancel&lpar;String message&comma; DialogInterface&period;OnClickListener okListener&rpar; &lbrace;&NewLine; new AlertDialog&period;Builder&lpar;MainActivity&period;this&rpar;&NewLine; &period;setMessage&lpar;message&rpar;&NewLine; &period;setPositiveButton&lpar;"GRANT"&comma; okListener&rpar;&NewLine; &period;setNegativeButton&lpar;"CANCEL"&comma; null&rpar;&NewLine; &period;create&lpar;&rpar;&NewLine; &period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void openSettingsDialog&lpar;&rpar; &lbrace;&NewLine;&NewLine; AlertDialog&period;Builder builder &equals; new AlertDialog&period;Builder&lpar;MainActivity&period;this&rpar;&semi;&NewLine; builder&period;setTitle&lpar;"Required Permissions"&rpar;&semi;&NewLine; builder&period;setMessage&lpar;"This app require permission to use awesome feature&period; Grant them in app settings&period;"&rpar;&semi;&NewLine; builder&period;setPositiveButton&lpar;"Take Me To SETTINGS"&comma; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;DialogInterface dialog&comma; int which&rpar; &lbrace;&NewLine; dialog&period;cancel&lpar;&rpar;&semi;&NewLine; Intent intent &equals; new Intent&lpar;Settings&period;ACTION&lowbar;APPLICATION&lowbar;DETAILS&lowbar;SETTINGS&rpar;&semi;&NewLine; Uri uri &equals; Uri&period;fromParts&lpar;"package"&comma; getPackageName&lpar;&rpar;&comma; null&rpar;&semi;&NewLine; intent&period;setData&lpar;uri&rpar;&semi;&NewLine; startActivityForResult&lpar;intent&comma; 101&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; builder&period;setNegativeButton&lpar;"Cancel"&comma; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;DialogInterface dialog&comma; int which&rpar; &lbrace;&NewLine; dialog&period;cancel&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; builder&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;&NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><strong>I hope this article gave you a good overview of Marshmallow permission model&period; Feel free to ask any queries &sol; doubts in comments section below&period;<&sol;strong><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><&sol;div>&NewLine;&NewLine;

Exit mobile version