Created
April 13, 2016 18:55
-
-
Save carloscarucce/e809093c0f0bd90e89c86857f823fd9d to your computer and use it in GitHub Desktop.
Obtem a localização do gps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xxnoobmanxx.util; | |
import android.Manifest; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.location.LocationProvider; | |
import android.os.Bundle; | |
import android.support.v4.app.ActivityCompat; | |
/** | |
* Created by carucce on 30/11/2015. | |
*/ | |
public class GPS { | |
private static GPS instance; | |
private Context mContext; | |
private LocationManager mLocationManager; | |
private Location mLocation = null; | |
private boolean started = false; | |
public static GPS getInstance(Context context) { | |
if (instance == null) { | |
instance = new GPS(context); | |
} | |
return instance; | |
} | |
/** | |
* | |
*/ | |
public void stop() { | |
if(!started) return; | |
try{ | |
started = false; | |
mLocationManager.removeUpdates( | |
mLocationListener | |
); | |
}catch (SecurityException e){ | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* | |
* @param context | |
*/ | |
private GPS(Context context){ | |
mContext = context; | |
} | |
/** | |
* | |
* @return | |
*/ | |
public Location getLocation(){ | |
return mLocation; | |
} | |
/** | |
* | |
* @return | |
*/ | |
public boolean hasPermission(){ | |
return ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED | |
|| ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED; | |
} | |
/** | |
* | |
*/ | |
public void start(){ | |
if(started) return; | |
try { | |
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); | |
LocationProvider provider = mLocationManager.getProvider(LocationManager.GPS_PROVIDER); | |
if(provider != null) { | |
started = true; | |
mLocation = mLocationManager.getLastKnownLocation(provider.getName()); | |
mLocationManager.requestLocationUpdates(provider.getName(), 1000, 1, mLocationListener); | |
} | |
} catch (SecurityException e){ | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* | |
*/ | |
private LocationListener mLocationListener = new LocationListener() { | |
@Override | |
public void onLocationChanged(Location location) { | |
mLocation = location; | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment