Skip to content

Instantly share code, notes, and snippets.

@dGorod
Created July 26, 2016 10:29
Show Gist options
  • Save dGorod/1ea2019fe3aceee71e160fcff63b9ba6 to your computer and use it in GitHub Desktop.
Save dGorod/1ea2019fe3aceee71e160fcff63b9ba6 to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
/**
* Util to check services availability.
*
* Created by:
* @author Dmytro Gorodnytskyi
* on 26-Jul-16.
*/
public class ServicesUtil {
public static final int PLAY_SERVICES_RESOLUTION_REQUEST = 990;
public static boolean isNetworkAvailable(@NonNull Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnected();
}
public static boolean isLocationAvailable(@NonNull Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean wifiEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return gpsEnabled || wifiEnabled;
}
public static boolean isGoogleServicesAvailable(@NonNull Activity activity) {
GoogleApiAvailability googleApis = GoogleApiAvailability.getInstance();
int resultCode = googleApis.isGooglePlayServicesAvailable(activity);
if(resultCode != ConnectionResult.SUCCESS) {
if (googleApis.isUserResolvableError(resultCode)) {
googleApis.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
}
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment