Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.lang.reflect.Method;

import android.content.Context;

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

public class Main {
    /**
     * This method is used to check whether GPRS is connected or not.
     * @param appContext
     * @return
     */
    public static boolean isGPRSConnected(Context appContext) {
        boolean isConnected = false;
        if (isMobileDataEnables(appContext)) {
            ConnectivityManager connMgr = (ConnectivityManager) appContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if (mobile != null && mobile.isAvailable() && mobile.isConnected()) {

                isConnected = true;
            }
            return isConnected;
        } else {
            return isConnected;
        }
    }

    /**
     * This method is used to check whether mobile data connection is enabled or disabled.
     * @param appContext
     * @return
     */
    public static boolean isMobileDataEnables(Context appContext) {
        boolean mobileDataEnabled = false; // Assume disabled
        ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            Class cmClass = Class.forName(cm.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true); // Make the method call able
            // get the setting for "mobile data"
            mobileDataEnabled = (Boolean) method.invoke(cm);
        } catch (Exception e) {
            System.err.println("isMobileDataEnables() : " + e);
        }
        return mobileDataEnabled;
    }
}