Skip to content

Instantly share code, notes, and snippets.

@Isabellle
Created August 19, 2016 09:30
Show Gist options
  • Save Isabellle/bff8611e36e2fd6b0876b631796c782f to your computer and use it in GitHub Desktop.
Save Isabellle/bff8611e36e2fd6b0876b631796c782f to your computer and use it in GitHub Desktop.
Manager to deal with GetLastKnownLocation and LocationListener
package ****
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.****.mobile.news.R;
import java.util.List;
/**
* Created by Isabelle Lepez on 18/08/16.
*/
public class LocatorManager{
public static final LocatorManager sInstance = new LocatorManager();
public static LocatorManager getInstance() {
return sInstance;
}
private LocatorManager() {
// Need to set the default constructor inaccessible because of the singleton pattern.
}
public LocationManager mLocationManager;
public LocationListener mLocationListener;
private Location location;
private Context mContext;
private boolean isGpsEnabled;
private boolean isNetworkEnabled;
private String mBestProvider;
public void init(Context context) {
mContext = context;
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}
private boolean canGetLocation(){
isGpsEnabled = false;
isNetworkEnabled = false;
isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGpsEnabled && !isNetworkEnabled){
displayDialogActivateGps();
return false;
}
return true;
}
public Location getLocation(){
if(canGetLocation()){
location = getLastKnownLocation();
mLocationListener = new OnLocationListener();
//noinspection MissingPermission
mLocationManager.requestLocationUpdates(mBestProvider, 0, 0, mLocationListener);
}
return location;
}
public void unregisterLocationListener(){
//noinspection MissingPermission
mLocationManager.removeUpdates(mLocationListener);
}
private void displayDialogActivateGps() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog
.setMessage(R.string.toast__geolocation__activate)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.setCancelable(true)
.show();
}
private Location getLastKnownLocation() {
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
//noinspection MissingPermission
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
mBestProvider = bestLocation.getProvider();
}
}
return bestLocation;
}
private class OnLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment