Monday, November 26, 2012

Using Telephony Manager In Android with Example

The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.

 Telephony Manager

Telephony Manager provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information. Applications can also register a listener to receive notification of telephony state changes.

You do not instantiate this class directly; instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE).

Android Custom Alert Dialog Example

 Some More Good  Android Topics
Customizing Toast In Android 
 Showing Toast for Longer Time
Customizing Checkboxes In Android  
Customizing Progress Bar
To learn Basic of Android Animation  go to  Android Animation Basics

 

 

Permission Required: 


To work with Telephony Manager and to read the phone details we need
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>           permission.So  add this permission in  your manifest file.

Accessing the Telephony Manager:

Have an  object of TelephonyMnager
TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

Get IMEI Number of Phone

         String IMEINumber=tm.getDeviceId();

 Get Subscriber ID

          String subscriberID=tm.getDeviceId();

  Get SIM Serial Number

            String SIMSerialNumber=tm.getSimSerialNumber();

 Get Network Country ISO Code

             String networkCountryISO=tm.getNetworkCountryIso();

 Get SIM Country ISO Code

               String SIMCountryISO=tm.getSimCountryIso();

 Get the device software version

               String softwareVersion=tm.getDeviceSoftwareVersion()

 Get the Voice mail number

               String voiceMailNumber=tm.getVoiceMailNumber();


 Get the Phone Type CDMA/GSM/NONE

/ /Get the type of network you are connected with 
            int phoneType=tm.getPhoneType();

            switch (phoneType)
            {
                    case (TelephonyManager.PHONE_TYPE_CDMA):

                               // your code
                                   break;
                    case (TelephonyManager.PHONE_TYPE_GSM) 

                               // your code                 
                                   break;
                    case (TelephonyManager.PHONE_TYPE_NONE):

                               // your code              
                                    break;
             }


 Find whether the Phone is in Roaming, returns true if in roaming

             boolean isRoaming=tm.isNetworkRoaming();
              if(isRoaming)
                      phoneDetails+="\nIs In Roaming : "+"YES";
              else
                     phoneDetails+="\nIs In Roaming : "+"NO";


Get the SIM state/Details

            int SIMState=tm.getSimState();
            switch(SIMState)
            {
                    case TelephonyManager.SIM_STATE_ABSENT :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_PIN_REQUIRED :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_PUK_REQUIRED :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_READY :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_UNKNOWN :
                        // your code
                        break;
           
            }





How to Get IMEI number of the Phone


Getting  Network Details


// Get connected network country ISO code
String networkCountry = telephonyManager.getNetworkCountryIso();


// Get the connected network operator ID (MCC + MNC)
String networkOperatorId = telephonyManager.getNetworkOperator();


// Get the connected network operator name
String networkName = telephonyManager.getNetworkOperatorName();


// Get the type of network you are connected with 

int networkType = telephonyManager.getNetworkType();
switch (networkType) {
case (TelephonyManager.NETWORK_TYPE_1xRTT) :"  Your Code ":
break;
case (TelephonyManager.NETWORK_TYPE_CDMA) :"  Your Code ":
break;
case (TelephonyManager.NETWORK_TYPE_EDGE) : "  Your Code ":
break;
case (TelephonyManager.NETWORK_TYPE_EVDO_0) :"  Your Code ":
break;





 

Getting SIM Details :


Using the Object of Telephony Manager class we can get the details like SIM Serial number, Country Code, Network Provider code and other Details.

int simState = telephonyManager.getSimState();
switch (simState) 

{
            case (TelephonyManager.SIM_STATE_ABSENT): break;
            case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): break;
            case (TelephonyManager.SIM_STATE_PIN_REQUIRED): break;
           case (TelephonyManager.SIM_STATE_PUK_REQUIRED): break;
           case (TelephonyManager.SIM_STATE_UNKNOWN): break;
           case (TelephonyManager.SIM_STATE_READY): 

            {
                         // Get the SIM country ISO code
                       String simCountry = telephonyManager.getSimCountryIso();
                         // Get the operator code of the active SIM (MCC + MNC)
                      String simOperatorCode = telephonyManager.getSimOperator();
                       // Get the name of the SIM operator
                      String simOperatorName = telephonyManager.getSimOperatorName();
                        // -- Requires READ_PHONE_STATE uses-permission --
                        // Get the SIM’s serial number

                       String simSerial = telephonyManager.getSimSerialNumber();

          }
}





 

New Advance Topics:
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation

 Beginning With Android
      Android : Introduction                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage
Working With Incoming Calls                                             DataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog



13 comments:

  1. Thank you Kamlesh,this Tutorial is Very Useful for me.

    ReplyDelete
  2. Hello Sir Kamlesh, i enjoy your tutorials a lot. am working on a school project, am required to trace the GPS location of a caller from google map, now am having a little challenge,which is the best way to get the caller's location details forwarded to my phone once he's call state changes to TelephonyManager.CALL_STATE_OFFHOOK ...
    i thought of querying his current location data and sending to a web service from where i can interact with it from my application.. i dont know which is the best way to handle this problem ,please help me out.

    ReplyDelete
  3. nice tutorials :)
    but i have a question i wanna the app send me alert after the sim card changed send me alert with phone state thanks :)

    ReplyDelete
  4. very nice Kamlesh but i wanna use it after the sim card is changed if the sim card changed show me automatically alert with sim card data after the sim card changed

    ReplyDelete
    Replies
    1. For that you need to Implement BootReciver which will execute when Device completes boot process.
      On bootcompleted, check if the previous SIM card number is same as the Current SIM card number.
      You can go through the topic http://learnandroideasily.blogspot.in/2013/07/bootcompleted-broadcastreceiver-in.html

      If you need more help, pls let me know.

      Delete
  5. its nice.. but can you please tell me about web services and how we can get data form the server...

    ReplyDelete
  6. Hello there, I want to create simple utility app which switch between first and second sim for voice call. I searched online but didn't got API. Please help

    ReplyDelete
  7. Hello, I’m happy to see some great articles on your site. Would you like to come to my site later? My site also has posts, comments and communities similar to yours. Please visit and take a look 경마사이트

    ReplyDelete
  8. I always like finding a site that gives you very good ideas because I like learning new stuff. Happy that I found your site because I greatly liked it and I anticipate your following post. A fantastic blog and i’ll come back again for more useful content…우리카지노
    (mm)

    ReplyDelete
  9. I checked out some of the articles that were posted on both your website and your blog, which I followed by going to both of those locations. Both of them impressed me as being interesting as well as educational. how-to-implement-IoT-based-energy-monitoring-in-businesses

    ReplyDelete