Wednesday 3 June 2015

Check Network state and type | Wifi | Mobile 2G/3G |

No comments
In this android example i show you some interesting code about network state. just write a simple android code to get network information.
  1. Check network connection type WiFi/Mobile data etc.
  2. Check state connected or disconnected
  3. Previously activated network
Before starting example we should know some thing about KEY and Meaning of connectivity manager API.The intent never contains any data or type information, but some possible keys are provided in the ConnectivityManager API:


KEY and MEANING:

EXTRA_EXTRA_INFO : Contains additional information about the network state.

EXTRA_IS_FAILOVER : A Boolean that indicates whether this network is used as a failover for another, previously active network.

EXTRA_NETWORK_INFO : The Network Info object containing all information about the current state of this network type.

EXTRA_NO_CONNECTIVITY :  A Boolean that is set to true if the device has no connectivity at all.
EXTRA_OTHER_NETWORK_INFO : The Network Info object containing all information about the current state of a possible alternative network type.

EXTRA_REASON : The reason of the connectivity change.

CREATE NEW ANDROID PROJECT

Step 1: Write code into activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/tvinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="@string/hello_world"
        android:textSize="20sp" />

</LinearLayout>


Step 2: Write code into MainActivity.java

package dev.androidapplink.connectionstatusapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends Activity {
private ConnectivityReceiver receiver = null;
private TextView txtNetworkInfo = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtNetworkInfo = (TextView) findViewById(R.id.tvinfo);
receiver = new ConnectivityReceiver();

registerReceiver(receiver, new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
}

@Override
protected void onDestroy() {
unregisterReceiver(receiver);

super.onDestroy();
}

private String getNetworkStateString(NetworkInfo.State state) {
String stateString = "Unknown";

switch (state) {
case CONNECTED:
stateString = "Connected";
break;
case CONNECTING:
stateString = "Connecting";
break;
case DISCONNECTED:
stateString = "Disconnected";
break;
case DISCONNECTING:
stateString = "Disconnecting";
break;
case SUSPENDED:
stateString = "Suspended";
break;
default:
stateString = "Unknown";
break;
}

return stateString;
}

private class ConnectivityReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// In this example i using " EXTRA_NETWORK_INFO " The NetworkInfo object containing all information about the current state of this network type

NetworkInfo info = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

if (null != info) {
String state = getNetworkStateString(info.getState());
String stateString = info.toString().replace(',', '\n');

String infoString = String.format(
"Network Type: %s\nNetwork State: %s\n\n%s",
info.getTypeName(), state, stateString);

Log.i("ConnTest", info.getTypeName());
Log.i("ConnTest", state);
Log.i("ConnTest", info.toString());

txtNetworkInfo.setText(infoString);
}
}
}
}

NOTE:Give permission in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Step 3: Run your project:


WiFi Connected Or Disconnected:




 Bobile data 2G and 3G connection:







No comments :

Post a Comment

Follow me Share