Presentation is loading. Please wait.

Presentation is loading. Please wait.

Permissions.

Similar presentations


Presentation on theme: "Permissions."— Presentation transcript:

1 Permissions

2 Android Permissions Beginning with Android 6.0 (Marshmallow, API level 23), users must grant certain permissions to an application at runtime, not simply when the application is installed. Streamlines the application installation process Gives the user more control over the application’s functionality user can grant some permissions and deny others User can revoke the permissions at any time e.g., by going to the application’s Settings screen But … This approach complicates things for application developers. ©SoftMoore Consulting

3 Normal and Dangerous Permissions
System permissions are divided into several protection levels, but from a developer perspective, the two most important protection levels are normal and dangerous permissions. Normal permissions require access to data or resources outside the application, but they pose very little risk to the user’s privacy or the operation of other applications. e.g., set an alarm or access the internet Dangerous permissions cover areas where an application needs data or resources that involve the user’s private information or that could potentially affect the operation of other applications. e.g., access to the user’s location or contact information ©SoftMoore Consulting

4 Declaring Permissions in the Manifest
Regardless of whether a permission is categorized as normal or dangerous, all permissions needed by an application must be declared in the manifest. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" package="edu.citadel.android.permission"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <application ... <activity </activity> </application> </manifest> ©SoftMoore Consulting

5 Checking for Permissions
An application that needs a dangerous permission must check whether that permission is granted every time it performs an operation that requires that permission. Even if a permission was granted previously, the user can still revoke the permission by going to the application’s Settings screen. To check if an application has a permission, call method ContextCompat.checkSelfPermission(). ©SoftMoore Consulting

6 Checking for Permissions (continued)
Example int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { makeCall(); } If the application does not have the permission, this method returns PERMISSION_DENIED, and the application must explicitly ask the user for permission. ©SoftMoore Consulting

7 Requesting Permissions
If an application doesn’t already have the permission it needs, it can call requestPermissions() to request the appropriate permissions. The application passes an array of the permissions it wants to request plus an integer request code to identify this permission request. The requestPermissions() method functions asynchronously. method returns immediately system presents a dialog box to the user ©SoftMoore Consulting

8 Permission Groups All dangerous permissions belong to permission groups. Examples Permission group CONTACTS includes permissions READ_CONTACTS, WRITE_CONTACTS, and GET_ACCOUNTS. Permission group PHONE includes permissions READ_PHONE_STATE, CALL_PHONE, READ_CALL_LOG, etc. An application requests a specific permission, but the system dialog box requests permission for an entire group; i.e., it does not describe the specific permission within the group that is being requested. If the user grants permission, then all permissions within the group are granted. ©SoftMoore Consulting

9 Example: Requesting Permissions
private static final int PERMISSION_REQUEST_CALL_PHONE = 100; ... // must check for permissions at runtime before placing a call int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { makeCall(); } else // request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CALL_PHONE); passes an array of requests so that more than one permission can be requested at the same time ©SoftMoore Consulting

10 Example: Requesting Permissions (continued)
©SoftMoore Consulting

11 Handling the Response to the Permission Request
When the user responds to a permissions dialog request, the system invokes the application’s onRequestPermissionsResult() method, passing it the user’s response and the request code that was initially passed to requestPermissions(). An application will need to override method onRequestPermissionsResult() to find out whether the permission was granted. ©SoftMoore Consulting

12 Example: Handling the Response to the Permission Request
@Override public void onRequestPermissionsResult(int String int[] grantResults) { if (requestCode == PERMISSION_REQUEST_CALL_PHONE) if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) makeCall(); } else ... // notify user that permission was denied value passed to method requestPermissions() ©SoftMoore Consulting

13 Permission Request Rationale
In some circumstances, you might want to help the user understand why an application needs a certain dangerous permission. One approach is to provide an explanation only if the user has already turned down that permission request. To help in these situations where the user might need an explanation, Android provides a utility method, shouldShowRequestPermissionRationale(). returns true if the app has requested this permission previously and the user denied the request usually called in method onRequestPermissionResult() if the permission was not granted ©SoftMoore Consulting

14 Example: Permission Request Rationale
private void checkCallPhonePermission() { int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) ... } else if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) ... // show explanation asynchronously; do not block UI thread ... // call method ActivityCompat.requestPermissions() after checking if permission has been granted but before requesting the permission ©SoftMoore Consulting

15 Example: Permission Request Rationale (continued)
private void checkCallPhonePermission() { // must check for permissions at runtime before placing a call int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) makeCall(); } else if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) // show explanation asynchronously -- do not block ui thread String message = " ... "; Toast toast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG); toast.show(); // request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CALL_PHONE); ©SoftMoore Consulting

16 Permission Usage Notes
Try not to overwhelm the user with permission requests. Consider using an intent. Rather than have an application perform a task, use an intent to ask another application to perform the task, and get results from the other application. Ask only for the permissions needed. Explain why the permissions are needed by the application; e.g., in a Toast or Snackbar, or in a separate tutorial. ©SoftMoore Consulting

17 Making a Phone Call in Android
/** * Initiates a telephone call to the specified phone number. */ private void makeCall() { try Uri uri = Uri.parse("tel:" + phoneNumToCall); Intent intent = new Intent(Intent.ACTION_CALL, uri); startActivity(intent); } catch (SecurityException ex) String errorMsg = "No permission to make phone call."; Log.e(LOG_TAG, errorMsg, ex); ©SoftMoore Consulting

18 Making a Phone Call in Android (continiued)
©SoftMoore Consulting

19 Relevant Links Normal and Dangerous Permissions Normal Permissions
Normal Permissions Requesting Permissions at Run Time Permissions Usage Notes Android Runtime Permissions Request Tutorial ©SoftMoore Consulting


Download ppt "Permissions."

Similar presentations


Ads by Google