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.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;

public class Main {
    /**
     * Returns true if the context has permission to access the network state and
     * access the internet.
     * 
     * @return True if the context has the correct permissions.
     */
    public static boolean hasNetworkPermissions(Context context) {

        // get permissions for accessing the internet and network state
        int internetPerm = context.checkCallingOrSelfPermission(Manifest.permission.INTERNET);
        int networkStatePerm = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);

        // are those permissions allowed
        boolean internetAllowed = internetPerm == PackageManager.PERMISSION_GRANTED;
        boolean networkAllowed = networkStatePerm == PackageManager.PERMISSION_GRANTED;

        // if they are check if we are connected to the network
        return (internetAllowed && networkAllowed);
    }
}