Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.Manifest;

import android.content.Context;

import android.content.pm.PackageManager;

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

public class Main {
    public static boolean isNetworkAvailable(Context ctx) {
        int networkStatePermission = ctx.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);

        if (networkStatePermission == PackageManager.PERMISSION_GRANTED) {

            ConnectivityManager mConnectivity = (ConnectivityManager) ctx
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            // Skip if no connection, or background data disabled
            NetworkInfo info = mConnectivity.getActiveNetworkInfo();
            if (info == null) {
                return false;
            }
            // Only update if WiFi
            int netType = info.getType();
            // int netSubtype = info.getSubtype();
            if ((netType == ConnectivityManager.TYPE_WIFI) || (netType == ConnectivityManager.TYPE_MOBILE)) {
                return info.isConnected();
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}