Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.content.pm.PackageManager;

import android.net.ConnectivityManager;

public class Main {
    public static boolean isOnline(Context context) {
        if (hasPermission(context, "android.permission.ACCESS_NETWORK_STATE") == false) {
            return true;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager.getActiveNetworkInfo() != null
                && connectivityManager.getActiveNetworkInfo().isAvailable()
                && connectivityManager.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean hasPermission(Context context, String name) {
        int res = context.checkCallingOrSelfPermission(name);
        return (res == PackageManager.PERMISSION_GRANTED);
    }
}