Get Network info using TelephonyManager

Using TelephonyManager, we can get cell phone network info; such as Network Operator (MCC+MNC), Network Operator Name, Network Type, Network Country Iso.

Permission READ_PHONE_STATE is required.

Get Network info using 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 textDeviceID = (TextView)findViewById(R.id.deviceid);
TextView textNetworkOperator = (TextView)findViewById(R.id.networkoperator);
TextView textNetworkOperatorName = (TextView)findViewById(R.id.networkoperatorname);
TextView textNetworkType = (TextView)findViewById(R.id.networktype);
TextView textNetworkCountryIso = (TextView)findViewById(R.id.networkcountryiso);

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

textDeviceID.setText(getDeviceID(telephonyManager));
textNetworkOperator.setText("Network Operator(MCC+MNC): " + telephonyManager.getNetworkOperator());
textNetworkOperatorName.setText("Network Operator Name: " + telephonyManager.getNetworkOperatorName());
textNetworkType.setText("Network Type: " + getNetworkType(telephonyManager));
textNetworkCountryIso.setText("Network Country Iso:" + telephonyManager.getNetworkCountryIso());
}

String getNetworkType(TelephonyManager phonyManager){
int type = phonyManager.getNetworkType();

switch(type){
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "NETWORK_TYPE_UNKNOWN";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "NETWORK_TYPE_GPRS";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "NETWORK_TYPE_EDGE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "NETWORK_TYPE_UMTS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "NETWORK_TYPE_HSDPA";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "NETWORK_TYPE_HSUPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "NETWORK_TYPE_HSPA";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "NETWORK_TYPE_CDMA";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "NETWORK_TYPE_EVDO_0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "NETWORK_TYPE_EVDO_0";
/* Since: API Level 9
* case TelephonyManager.NETWORK_TYPE_EVDO_B:
* return "NETWORK_TYPE_EVDO_B";
*/
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "NETWORK_TYPE_1xRTT";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "NETWORK_TYPE_IDEN";
/* Since: API Level 11
* case TelephonyManager.NETWORK_TYPE_LTE:
* return "NETWORK_TYPE_LTE";
*/
/* Since: API Level 11
* case TelephonyManager.NETWORK_TYPE_EHRPD:
* return "NETWORK_TYPE_EHRPD";
*/
default:
return "unknown";

}
}

String getDeviceID(TelephonyManager phonyManager){

String id = phonyManager.getDeviceId();

if (id == null){
id = "not available";
}

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

case TelephonyManager.PHONE_TYPE_GSM:
return "GSM: IMEI=" + id;

case TelephonyManager.PHONE_TYPE_CDMA:
return "CDMA: MEID/ESN=" + id;

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

default:
return "UNKNOWN: ID=" + id;
}

}
}


<?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/deviceid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/networkoperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/networkoperatorname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/networktype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/networkcountryiso"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Remark:
Getting line number by calling the method getLine1Number() is not always work!
You can check by pressing MENU > Settings > About Phone > Status > My phone nmber = unknown
That means this function have been disabled, even the API is provided.

0 Response to "Get Network info using TelephonyManager"

Posting Komentar