By default, Android system doesn't associate any permissions with an android application that would adversely impact the user.
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.
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.
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.
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.
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.
Result of the this sample is...
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 AndroidManifest.java, I've just added