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: currentNetworkIsWifi
     * @Description: TODO
     */
    public static boolean currentNetworkIsWifi(Context context) {
        if (checkPermission(context, "android.permission.ACCESS_WIFI_STATE"))
            ;
        Context contextApplication = context.getApplicationContext();
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getTypeName().equals("WIFI") && info[i].isConnected())
                        return true;
                }
            }
        }
        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;
    }
}