Android permissions


Downlod Android system permission sample

By default, Android system doesn't associate any permissions with an android application that would adversely impact the user.

Security Architecture

In android security architecture, no application, by default, gets permission to perform any operations that would adversely impact other applications, the operating system, or the user. This includes reading or writing the user's private data (such as contacts or emails), reading or writing another application's files, performing network access, keeping the device awake, and so on.

Using Permissions

If your application need to use any such permission, that can adversely affect other applications or system then you must lists all such permission in AndroidManifest.xml.

till api level 22 or lower, Android system ask user to grant permission listed in AndroidManifest.xml at install time.

From Andoid 6.0(Api level 23), android has changed the permission strategy. Android system has devided System permissions in 2 parts: Normal permissions and Dangerous permissions. If permissions listed in manifest are normal permissions then android system grants such permissions automatically but if permissions are dangerous type, then application must ask user to grant permission at runtime.

Normal permissions

Those permissions that don't pose much risk to the user's privacy or the device's operation, such permissions are normal permissions and android system grants permission automatically.

Normal permissions list

  1. android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
  2. android.permission.ACCESS_NETWORK_STATE
  3. android.permission.ACCESS_NOTIFICATION_POLICY
  4. android.permission.ACCESS_WIFI_STATE
  5. android.permission.ACCESS_WIMAX_STATE
  6. android.permission.BLUETOOTH
  7. android.permission.BLUETOOTH_ADMIN
  8. android.permission.BROADCAST_STICKY
  9. android.permission.CHANGE_NETWORK_STATE
  10. android.permission.CHANGE_WIFI_MULTICAST_STATE
  11. android.permission.CHANGE_WIFI_STATE
  12. android.permission.CHANGE_WIMAX_STATE
  13. android.permission.DISABLE_KEYGUARD
  14. android.permission.EXPAND_STATUS_BAR
  15. android.permission.FLASHLIGHT
  16. android.permission.GET_ACCOUNTS
  17. android.permission.GET_PACKAGE_SIZE
  18. android.permission.INTERNET
  19. android.permission.KILL_BACKGROUND_PROCESSES
  20. android.permission.MODIFY_AUDIO_SETTINGS
  21. android.permission.NFC
  22. android.permission.READ_SYNC_SETTINGS
  23. android.permission.READ_SYNC_STATS
  24. android.permission.RECEIVE_BOOT_COMPLETED
  25. android.permission.REORDER_TASKS
  26. android.permission.REQUEST_INSTALL_PACKAGES
  27. android.permission.SET_TIME_ZONE
  28. android.permission.SET_WALLPAPER
  29. android.permission.SET_WALLPAPER_HINTS
  30. android.permission.SUBSCRIBED_FEEDS_READ
  31. android.permission.TRANSMIT_IR
  32. android.permission.USE_FINGERPRINT
  33. android.permission.VIBRATE
  34. android.permission.WAKE_LOCK
  35. android.permission.WRITE_SYNC_SETTINGS
  36. com.android.alarm.permission.SET_ALARM
  37. com.android.launcher.permission.INSTALL_SHORTCUT
  38. com.android.launcher.permission.UNINSTALL_SHORTCUT

Dangerous permissions

Those permissions that could potentially affect the user's privacy or the device's normal operation are known as dangerous permissions.

If the device is running Android 6.0 (API level 23) or higher, and the targetSdkVersion of the application is 23 or higher, then app must request user to grant permissions at runtime.

Dangerous permissions list

  1. com.android.launcher.permission.READ_CALENDAR
  2. com.android.launcher.permission.WRITE_CALENDAR
  3. com.android.launcher.permission.CAMERA
  4. com.android.launcher.permission.READ_CONTACTS
  5. com.android.launcher.permission.WRITE_CONTACTS
  6. com.android.launcher.permission.GET_ACCOUNTS
  7. com.android.launcher.permission.ACCESS_FINE_LOCATION
  8. com.android.launcher.permission.ACCESS_COARSE_LOCATION
  9. com.android.launcher.permission.RECORD_AUDIO
  10. com.android.launcher.permission.READ_PHONE_STATE
  11. com.android.launcher.permission.CALL_PHONE
  12. com.android.launcher.permission.READ_CALL_LOG
  13. com.android.launcher.permission.WRITE_CALL_LOG
  14. com.android.launcher.permission.ADD_VOICEMAIL
  15. com.android.launcher.permission.USE_SIP
  16. com.android.launcher.permission.PROCESS_OUTGOING_CALLS
  17. com.android.launcher.permission.BODY_SENSORS
  18. com.android.launcher.permission.SEND_SMS
  19. com.android.launcher.permission.RECEIVE_SMS
  20. com.android.launcher.permission.READ_SMS
  21. com.android.launcher.permission.RECEIVE_WAP_PUSH
  22. com.android.launcher.permission.RECEIVE_MMS
  23. com.android.launcher.permission.READ_EXTERNAL_STORAGE
  24. com.android.launcher.permission.WRITE_EXTERNAL_STORAGE

Getting phone number of the android phone

Due to security reasons, we can't get mobile number of a android device programmatically. Here is the code to get mobile number...

TelephonyManager mgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = mgr.getLine1Number();

The above code can return correct mobile number but it can also return null or "" or even "???????", and it can return a stale phone number that is no longer valid. So, if we need something that uniquely identifies the device, we should use getDeviceId() instead.

Lets try to implement a sample to get device information in android device.


Example

Result of the this sample is...

Device information in android

Steps

  1. Create an Android application and name it as android-device-info under a package com.apnatutorials.android_device_info with blank activity.
  2. Replace the source of MainActivity.java with source of MainActivity.java of the sample
  3. Add in yout AndroidManifest.xml, just above the application element.
  4. Replace your activity_main.xml source with activity_main.xml of the sample
  5. Replace your strings.xml with sample's strings
  6. Build and run on your device

MainActivity.java

string.xml

activity_main.xml

AndroidManifest.xml


Source explanation

Source code of above sample is straight forward. Lets try to understand code of all attached files.

MainActivity.java : Before Android 6.0( Api level 23), user was granting permission at the time of app installation. But from Adroid 6.0(Api level 23) and onward, application need to request permission that are in dangerous nature at run time. I will explain requesting and granting user's permission at run time in seprate thread.

Inside onCreate method, I am checking if sdk version of user's device is greater or equals to 23, then I am initiating permission request at runtime by calling requestPermission() method, In other case we don't need permission request.

Once user grants or denies permission request, android system calls onRequestPermissionsResult method. I am checking wether user has granted permission, if so then string permission status in a variable.

showDeviceInfo method is called when user click on button, here I am checking permission status showing message accordingly.

In string.xml I stored string resource of my app

In activity_main.xml I have used a simple Relative layout which hold a child button. I've specified which method will be called onClick of that button.

In AndroidManifest.java, I've just added to tell android system that application will use READ_PHONE_STATE permission.

Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.