Monday, July 30, 2012

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>

No comments:

Post a Comment