Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Getting Phone Details.

Author: Rachee Singh
Published? false -- FormatLanguage: W

Problem:

Some Android applications require the Android device's IMEI number, the phone number or the phone type (GSM/CDMA).

Solution:

We use an instance of the TelephonyManager class to obtain these details.

Discussion:

We obtain an instance of the TelephoneManager class to gain access to telephony related information:

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

To get the IMEI number, the phone type and the phone number we use inbuilt functions and set the appropriate text to the TextViews defined in the sample app.


        String IMEI = telephonyManager.getDeviceId();
        
        imei.setText("IMEI Number: "+ IMEI);
        
        int phoneType = telephonyManager.getPhoneType();
        
        if(phoneType == TelephonyManager.PHONE_TYPE_CDMA) {
        	
        	type.setText("Phone Type: CDMA");
        	
        } else if(phoneType == TelephonyManager.PHONE_TYPE_GSM){
        	
        	type.setText("Phone Type: GSM");
        	
        }
        String phoneNo = telephonyManager.getLine1Number();

        phoneNumber.setText("Phone Number: "+phoneNo);
        

Download:

The source code for this project can be downloaded from https://docs.google.com/leaf?id=0B_rESQKgad5LOTIyZmYyZGUtYzdiZS00YTVkLWJiOTAtYzQzNjI1ZGE2ZWUz&hl=en_US.
No records found.