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.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {

    public static boolean isMobileAvailable(Context context) {
        int type = ConnectivityManager.TYPE_MOBILE;
        return isAvailableByType(context, type);
    }

    private static boolean isAvailableByType(Context context, int type) {
        if (context != null) {
            ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (manager != null) {
                NetworkInfo[] networkInfos = manager.getAllNetworkInfo();
                for (int i = 0; i < networkInfos.length; i++) {
                    if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
                        if (networkInfos[i].getType() == type) {
                            return true;
                        }
                    }
                }

            }
        }
        return false;
    }
}