Android network type

In this Android tip, I am going to show you how to check network type of your Android phone. The network type indicates the type of your phone's available data connection. The network type can be 4G, 3G, 2G, or GPRS.
To achieve this goal, you can use the TelephonyManager class. It can be used to determine telephony services and states, as well as to access some types of subscriber information. To obtain an instance of the TelephonyManager, you need to use the getSystemService(Context.TELEPHONY_SERVICE) method of your current Activity. To check the network type available in an Android phone, you use constant names defined in the TelephonyManager. For example, the constant TelephonyManager.NETWORK_TYPE_HSDPA tells you that the network type is 2G. The checkNetworkType() method below can be called to check whether the network type is 2G, 3G,4G or GPRS. The detected network type is displayed in a Toast message.
package com.example.androidexample;


 import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);  
     checkNetworkType();
   }
  
   public void checkNetworkType(){
   TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

   if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA)) {
  
   Toast.makeText(this, "3G network type", Toast.LENGTH_LONG).show();
  
  
   } else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPAP)) {
   Toast.makeText(this, "4G network type", Toast.LENGTH_LONG).show();
  
   } else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS)) {
   Toast.makeText(this, "GPRS network type", Toast.LENGTH_LONG).show();

   } else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE)) {
   Toast.makeText(this, "2G network type", Toast.LENGTH_LONG).show();

   }
   }

}

Android-network-type

Posted by: Dara | post date: 10-24-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.