Download presentation
Presentation is loading. Please wait.
Published byGinger Lynch Modified over 9 years ago
1
Cosc 4735 Permissions Asking for them in API 23+
2
Permissions. Just as always, we have put the permissions requests in the manifest.xml Example: The last two are in the “dangerous” group and we are going to have to ask the user to allow it as an extra step in our code.
3
Runtime Permissions The users will now be able to manage some of the App’s permissions. Permissions are grouped into two categories – Dangerous These where the app wants access to a user’s private information and the app must request access from the user. – Such as calendar, contacts, camera, mic, location, phone, sensors, sms, and storage – http://developer.android.com/guide/topics/security/permissions.html#perm- groups http://developer.android.com/guide/topics/security/permissions.html#perm- groups – normal Very little risk to a user’s private information and app is granted it automatically (assuming it requests it in the manifest.xml correctly) – internet access as example. So you can not remove an app’s access to the network. – http://developer.android.com/guide/topics/security/normal-permissions.html http://developer.android.com/guide/topics/security/normal-permissions.html Note: you can also add your app launch to the dangerous group.
4
(current as of 1/11/2016) Dangerous Group Permission GroupPermissions CALENDAR READ_CALENDAR WRITE_CALENDAR CAMERA CONTACTS READ_CONTACTS WRITE_CONTACTS GET_ACCOUNTS LOCATION ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION MICROPHONE RECORD_AUDIO PHONE READ_PHONE_STATE CALL_PHONE READ_CALL_LOG WRITE_CALL_LOG ADD_VOICEMAIL USE_SIP PROCESS_OUTGOING_CALLS SENSORS BODY_SENSORS SMS SEND_SMS RECEIVE_SMS READ_SMS RECEIVE_WAP_PUSH RECEIVE_MMS STORAGE READ_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE http://developer.android.com/guide/topics/security/permissions.html#perm-groups
5
Checking permission. Any time you use a permission that could be revoked by the user, you MUST first check to see you have the permission or not. – If not, then ask for it. – Else don’t use it! – Example we are going to use the GPS, which has the Manifest.permission.ACCESS_FINE_LOCATION.
6
Coding it. Checking to see if I already have permission using the checkSelfPermission(…) method. if (ActivityCompat.checkSelfPermission( getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) { //I don’t have the user permission yet. So ask for it. } else { //I’m good, I can use the GPS. } There is a flow problem with the code, that we are going to have to fix a bit later.
7
Requesting permission To request a permission from the user, we use the requestPremissions(context, permission, int constant) method. – This method allows us to ask for multiple permissions at the same time. – The request returns async to onRequestPermissionsResult method. ActivityCompat.requestPermissions( getActivity(), new String[]{ Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_FINE_ACCESS); Add more permission to the String[] to request multiple permissions at the same time.
8
results Callback received when a permissions request has been completed. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_FINE_ACCESS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay call the code again to start the GPS in this example. } else { // permission denied, boo! Disable this feature or close the app. } return; } // other 'case' lines to check for other permissions this app might request }
9
Multiple permission request Say I asked for both fine and course at the same time. ActivityCompat.requestPermissions( getActivity(), new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_ACCESS); Then in the result, I need to use the both – String permissions[] and int[] grantResults Array of permissions ie Manifest.permission.ACCESS_FINE_LOCATION and same position in the grantResults if permission was granted.
10
Program flow control Because of the Async request/result You are going to have to make your code in method calls – if they don’t already have permission, then the code can be easily restarted – Or code can be easily skipped.
11
Why every time? Because between uses or if the app is in the background – The user may turn off permission – In settings-> app permission.
12
References http://developer.android.com/training/permis sions/requesting.html http://developer.android.com/training/permis sions/requesting.html
13
Q A &
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.