Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;
import android.content.pm.PackageManager;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {

    public static String[] getNetworkState(Context pContext) {
        String[] type = new String[2];
        type[0] = "Unknown";
        type[1] = "Unknown";
        if (pContext.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE",
                pContext.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
            ConnectivityManager localConnectivityManager = (ConnectivityManager) pContext
                    .getSystemService("connectivity");
            if (localConnectivityManager == null)
                return type;

            NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
            if ((localNetworkInfo1 != null) && (localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED)) {
                type[0] = "Wi-Fi";
                type[1] = localNetworkInfo1.getSubtypeName();
                return type;
            }
            NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
            if ((localNetworkInfo2 == null) || (localNetworkInfo2.getState() != NetworkInfo.State.CONNECTED))
                type[0] = "2G/3G";
            type[1] = localNetworkInfo2.getSubtypeName();
            return type;
        }
        return type;
    }
}