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)
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.
Hello Eric,
ReplyDeleteThis 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
try this:
ReplyDeleteContext.getSystemService(Context.TELEPHONY_SERVICE)
Using
ReplyDeleteContext.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
thanks, but how can i check if the phone supports both GSM and CDMA networks
ReplyDelete