How to get device information in android


Downlod how to get device Info in android sample

Sometimes we need to get device information like OS version, device, manufacturer etc. Such information we can get using android.os.Build class. To get phone related detail like IEMI, IMSI, deviceId, mobile number we can use TelephonyManager. To get phone related information we must request READ_PHONE_STATE permission in AndroidManifest.xml.

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.