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.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.util.Log;

public class Main {
    public static boolean ensureWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo == null || !netInfo.isConnectedOrConnecting())
            return false;
        // always OK if we're on wifi
        if (netInfo.getType() == ConnectivityManager.TYPE_WIFI)
            return true;
        // check for wifi only pref
        if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("wifiPref", true)) {
            Log.d("Podax", "Not downloading because Wifi is required and not connected");
            return false;
        }
        // check for 3g data turned off
        if (!netInfo.isConnected()) {
            Log.d("Podax", "Not downloading because background data is turned off");
            return false;
        }

        return true;
    }
}