Created
April 4, 2015 05:50
-
-
Save quynguyen3490/854d09996522d8e629d3 to your computer and use it in GitHub Desktop.
Get Current Location Android
This file contains 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.quynguyenblog.mymap; | |
import android.app.AlertDialog; | |
import android.app.Service; | |
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 android.os.IBinder; | |
import android.provider.Settings; | |
public class GPSTracking extends Service implements LocationListener { | |
private final Context mContext; | |
boolean isGPSEnabled = false; | |
boolean isNetworkEnabled = false; | |
private boolean canGetLocation = false; | |
Location mLocation; | |
private double lat; | |
private double lng; | |
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10; | |
private static final long MIN_TIME_BW_UPDATE = 1000*60*1; | |
protected LocationManager locationManager; | |
public GPSTracking(Context context) { | |
this.mContext = context; | |
getLocation(); | |
} | |
public Location getLocation(){ | |
try{ | |
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); | |
isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER); | |
isNetworkEnabled = locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER); | |
if(!isGPSEnabled && !isNetworkEnabled){ | |
//no network or gps | |
}else{ | |
setCanGetLocation(true); | |
if(isNetworkEnabled){ | |
locationManager.requestLocationUpdates( | |
LocationManager.NETWORK_PROVIDER, | |
MIN_TIME_BW_UPDATE, | |
MIN_DISTANCE_CHANGE_FOR_UPDATE, | |
this | |
); | |
if(locationManager!=null){ | |
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
if(mLocation!=null){ | |
setLat(mLocation.getLatitude()); | |
setLng(mLocation.getLongitude()); | |
} | |
} | |
} | |
if (isGPSEnabled) { | |
if (mLocation == null) { | |
locationManager.requestLocationUpdates( | |
LocationManager.GPS_PROVIDER, | |
MIN_TIME_BW_UPDATE, | |
MIN_DISTANCE_CHANGE_FOR_UPDATE, this); | |
if (locationManager != null) { | |
mLocation = locationManager | |
.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
if (mLocation != null) { | |
setLat(mLocation.getLatitude()); | |
setLng(mLocation.getLongitude()); | |
} | |
} | |
} | |
} | |
} | |
}catch (Exception e){ | |
} | |
return mLocation; | |
} | |
public void showAlertDialog(){ | |
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); | |
alertDialog.setTitle("GPS is settings"); | |
alertDialog.setMessage("GPS is not enabled. Do you want go to settings menu?"); | |
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
mContext.startActivity(intent); | |
} | |
}); | |
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.cancel(); | |
} | |
}); | |
alertDialog.show(); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
// TODO: Return the communication channel to the service. | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
public double getLat() { | |
return lat; | |
} | |
public void setLat(double lat) { | |
this.lat = lat; | |
} | |
public double getLng() { | |
return lng; | |
} | |
public void setLng(double lng) { | |
this.lng = lng; | |
} | |
public boolean isCanGetLocation() { | |
return canGetLocation; | |
} | |
public void setCanGetLocation(boolean canGetLocation) { | |
this.canGetLocation = canGetLocation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment