Android How to - Check Network state








The following code shows how to Check Network state.

Example

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2s.myapplication3.app" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="java2s.com"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.java2s.myapplication3.app.MainActivity"
            android:label="java2s.com"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

res\layout\main.xml

<?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"
    />
</LinearLayout>

Reachability.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.net.InetAddress;
import java.net.UnknownHostException;
/*from  ww  w  .ja va  2  s .  c  om*/
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Reachability extends Activity {
    
    ReachabilityManager mReach;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        mReach = ReachabilityManager.getInstance(this);
        mReach.isHostReachable("209.85.227.104")
        Toast.makeText(this, "Network "+mReach.isNetworkReachable()+"\nGoogle "+mReach.isHostReachable(0xD155E368), Toast.LENGTH_SHORT).show();
    }
}
class ReachabilityManager {

    private ConnectivityManager mManager;
    
    private static ReachabilityManager instance;
    
    public static synchronized ReachabilityManager getInstance(Context context) {
        if(instance == null) {
            instance = new ReachabilityManager(context);
        }
        return instance;
    }
    
    private ReachabilityManager(Context context) {
        mManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    public boolean isHostReachable(String hostName) {
        try {
            byte[] ipv4 = InetAddress.getByName(hostName).getAddress();
            int address = ipv4[0];
            address += ipv4[1] << 8;
            address += ipv4[2] << 16;
            address += ipv4[3] << 24;
            return isHostReachable(address);
        } catch (UnknownHostException e) {
            return false;
        }
    }
    
    public boolean isHostReachable(int hostAddress) {
        return ( mManager.requestRouteToHost(ConnectivityManager.TYPE_WIFI, hostAddress)
                || mManager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE, hostAddress) );
    }
    
    public boolean isNetworkReachable() {
        NetworkInfo current = mManager.getActiveNetworkInfo();
        if(current == null) {
            return false;
        }
        return (current.getState() == NetworkInfo.State.CONNECTED);
    }
}
null