Android Context Get getLocationProvider(Context context)

Here you can find the source of getLocationProvider(Context context)

Description

get Location Provider

Declaration

public static LocationProvider getLocationProvider(Context context) 

Method Source Code

//package com.java2s;
import android.content.Context;

import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {
    public static LocationProvider getLocationProvider(Context context) {
        if (context != null) {
            LocationProvider networkProvider = null;
            LocationProvider gpsProvider = null;

            // Get location manager
            LocationManager lm = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);

            // Get network and GPS provider if available
            if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                networkProvider = lm/*  w  ww.  j  av a  2s  .  c om*/
                        .getProvider(LocationManager.NETWORK_PROVIDER);
            }
            if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                gpsProvider = lm.getProvider(LocationManager.GPS_PROVIDER);
            }

            if (networkProvider != null && isNetworkConnected(context)) {
                // Use network provider if it's available and network is
                // connected
                return networkProvider;
            } else if (gpsProvider != null) {
                // Otherwise use GPS provider
                return gpsProvider;
            } else {
                // But never use passive provider as it's too slow
                return null;
            }
        } else {
            return null;
        }
    }

    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

            return activeNetwork != null
                    && activeNetwork.isConnectedOrConnecting();
        } else {
            return false;
        }
    }
}

Related

  1. getIntSPR(String key, Context context)
  2. getLastKnownLocation(Context context)
  3. getLineErrorPaint(Context context)
  4. getLinePaint(Context context)
  5. getLineThickness(Context context)
  6. getLongSPR(String key, Context context)
  7. getMemoryClass(Context context)
  8. getMinFontSize(Context context)
  9. getMobilityPackageInfo(Context context)