Jun 2, 2011

Get phone type using android.telephony.TelephonyManager

android.telephony.TelephonyManager is a class providing 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).

To get the phone type of the ndroid device, we can use the method getPhoneType(), it returns a constant indicating the device phone type. This indicates the type of radio used to transmit voice calls.
  • TelephonyManager.PHONE_TYPE_NONE
  • TelephonyManager.PHONE_TYPE_GSM
  • TelephonyManager.PHONE_TYPE_CDMA
  • TelephonyManager.PHONE_TYPE_SIP (API Level 11)


Get phone type using android.telephony.TelephonyManager

package com.AndroidTelephonyManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class AndroidTelephonyManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textPhoneType = (TextView)findViewById(R.id.phonetype);

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

textPhoneType.setText(getPhoneType(telephonyManager));

}

String getPhoneType(TelephonyManager phonyManager){
int phoneType = phonyManager.getPhoneType();
switch(phoneType){
case TelephonyManager.PHONE_TYPE_NONE:
return "NONE";

case TelephonyManager.PHONE_TYPE_GSM:
return "GSM";

case TelephonyManager.PHONE_TYPE_CDMA:
return "CDMA";

/*
* for API Level 11 or above
* case TelephonyManager.PHONE_TYPE_SIP:
* return "SIP";
*/

default:
return "UNKNOWN";
}

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/phonetype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Related Post:
- Get ANDROID_ID
- Get unique device ID (IMEI, MEID or ESN) of Android phones.

4 comments:

  1. Hello Eric,

    This is very informative article.

    However while using your code, I face a little problem.

    For the following line of code

    //retrieve a reference to an instance of TelephonyManager
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

    My compiler isn't able to find getSystemService

    It shows this error "The method getSystemService(String) is undefined for the type BluetoothMessageAccessor" where BluetoothMessageAccessor is my class name.
    This can sound a stupid question to you, but I am new to android and java both the technologies.

    Thanks in advance for your help.
    Prady

    ReplyDelete
  2. try this:

    Context.getSystemService(Context.TELEPHONY_SERVICE)

    ReplyDelete
  3. Using

    Context.getSystemService(Context.TELEPHONY_SERVICE)

    did not work because it says getSystemService is not a static method.
    But it gave me a hint at using Context that is being received from some activity in my class' constructor and this worked.

    I did this :

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

    Thanks a lot. :)

    Prady

    ReplyDelete
  4. thanks, but how can i check if the phone supports both GSM and CDMA networks

    ReplyDelete

Infolinks In Text Ads