Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;

import android.content.pm.PackageManager;

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

public class Main {
    static Context collectUtilContext;

    /**
     * @param context
     * @return boolean
     * @throws
     * @Title: isNetworkAvailable
     * @Description: TODO
     */
    public static boolean isNetworkAvailable(Context context) {
        if (checkPermission(context, "android.permission.INTERNET")) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null && ni.isAvailable()) {
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * @param context
     * @param permission
     * @return boolean
     * @Title: checkPermission
     * @Description: TODO
     */
    public static boolean checkPermission(Context context, String permission) {

        collectUtilContext = context;
        PackageManager pm = context.getPackageManager();
        return pm.checkPermission(permission,
                collectUtilContext.getPackageName()) == PackageManager.PERMISSION_GRANTED;
    }
}