i need to find current location, either with network or gps.
i searched over internet and in stackoverflow but i cann't find what's wrong with this code.
i also set this parameter in manifest:
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
so here is my code:
LocationManager locationManager;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
findCurrentLocation();
//some other stuff here
}
public void findCurrentLocation()
{
boolean networkEnabled = false;
boolean gpsEnabled = false;
currentLocation = getLastKnownLocation();
if(currentLocation == null)
{
currentLocation = new Location("sometext");
locationFindDialog = ProgressDialog.show(this, "Find Your Location", "Please Wait", false);
try
{
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
catch(Exception e)
{
}
try
{
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
catch(Exception e)
{
}
if((networkEnabled == false) && (gpsEnabled == false))
{
locationFindDialog.dismiss();
showMessage("we need gps or sim card(or network) to find your location");
//show message
//you need at least one provider
return;
}
if(networkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);
}
if(gpsEnabled)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
}
}
private final LocationListener networkLocationListener = new LocationListener()
{
#Override
public void onLocationChanged(Location location)
{
currentLocation.set(location);
//unregister listener
locationManager.removeUpdates(this);
locationManager.removeUpdates(gpsLocationListener);
locationFindDialog.dismiss();
}
#Override
public void onProviderDisabled(String provider)
{
}
#Override
public void onProviderEnabled(String provider)
{
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
};
private final LocationListener gpsLocationListener = new LocationListener()
{
#Override
public void onLocationChanged(Location location)
{
currentLocation.set(location);
//unregister listener
locationManager.removeUpdates(this);
locationManager.removeUpdates(networkLocationListener);
locationFindDialog.dismiss();
}
#Override
public void onProviderDisabled(String provider)
{
}
#Override
public void onProviderEnabled(String provider)
{
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
};
private Location getLastKnownLocation()
{
Location location = null;
List<String> providers = locationManager.getAllProviders();
for(String provider : providers)
{
location = locationManager.getLastKnownLocation(provider);
if(location != null)
{
return location;
}
}
return null;
}
no mater how much i wait, the app don't give me the location.
what's wrong with this code and how i can fix it?
finally i found a solution for this problem, the idea is finding cellId and lac manually, then send them to google secret API, and find latitude and longitude of cell tower, it's not too accurate (because it's cell tower location not user location), but enough for me.
refer to this address for more info:
http://sunil-android.blogspot.com/2013/01/convert-celllocation-to-real-location.html
Related
In my application's splash screen, I'm starting to listen the location via gps or network until my second activity opens.
my network location listener:
private LocationListener locationListenerNetwork = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
newLocationReceived(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
my gps location listener:
private LocationListener locationListenerGPS = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
newLocationReceived(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
Inside onCreate I call getLocation method, for permissions I use rxPermission:
#SuppressLint("MissingPermission")
private void getLocation() {
rxPermissions
.request(Manifest.permission.ACCESS_FINE_LOCATION)
.subscribe(granted -> {
if (granted) {
boolean gpsEnabled, networkEnabled;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled && locationListenerGPS != null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0f, locationListenerGPS);
}
if (networkEnabled && locationListenerNetwork != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListenerNetwork);
}
}
} else {
Timber.e("LOCATION PERMISSION DENIED");
}
});
}
When new location is received I call:
private void newLocationReceived(Location location) {
if (locationManager != null) {
locationManager.removeUpdates(locationListenerNetwork);
locationManager.removeUpdates(locationListenerGPS);
}
locationManager = null;
this.location = location;
}
And in order to avoid memory leaks I call this method in onPause, onStop and onDestroy:
private void stopLocationListener() {
if (locationManager != null) {
locationManager.removeUpdates(locationListenerNetwork);
locationManager.removeUpdates(locationListenerGPS);
locationManager = null;
}
if (locationListenerNetwork != null) locationListenerNetwork = null;
if (locationListenerGPS != null) locationListenerGPS = null;
}
However, In order to LeakCanary I still get memory leaks in location manager. How can I resolve this? Also sometimes My splash screen passes the progresses too fast and my runtime permission can stay at background too
I'm trying to program an application which is using the current location from the user and calculating the distance and writes it into my listview.
The location doesn't have to be very accurate and i only want to fetch a new location when the list is refreshed or on app start, not continously.
My problem is that the locationlistener with gps takes too long to find a location and i have to update my list a lot before it is showing the right distance.
I was thinking about implementing a background task which gets the location and updates the list automatically when it found the position. Would that be a solution?
Is there any option to get a location faster, even if it is not as accurate as gps?
what i have so far on my location listener:
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lng = location.getLongitude();
myLoc.setLatitude(lat);
myLoc.setLongitude(lng);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
in this method i'm calling the locationmanager and listener and creating the listview with the distance
public void getList(){
locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
//... creating the list with distance and so on
}
i hope you can give me some hints how i can implement this that i will work as described above or tell me what i should use instead.
thanks :)
1). You can use LocationManager.NETWORK_PROVIDER
This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.
eg:- locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener);
2). If you want to use background task then use this service
public class LocationFinder extends Service {
public static double lat, lng;
LocationManager locationManager;
public void onDestroy() {
super.onDestroy();
if (locationManager != null && locationListener != null) {
locationManager.removeUpdates(locationListener);
}
}
#Override
public void onCreate() {
Log.v("location", "===>location ed onCreate " + lat);
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.v("location", "===>location ed onStartCommand " + lat + "==>" + lng);
new Handler().post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
getLocation();
}
});
return START_STICKY;
}
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void getLocation() {
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
if (locationManager != null) {
String provider = locationManager.getBestProvider(criteria, true);
if (provider != null) {
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 500, 50, locationListener);
} else {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener);
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 50, locationListener);
} else if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 500, 50, locationListener);
}
}
}
}
private void updateWithNewLocation(Location location) {
if (location != null) {
Log.v("location", "===>location ed " + lat);
lat = location.getLatitude();
lng = location.getLongitude();
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
I need a method to simply get latitude and longitude. I'm developing my first app... can you tell me if i'm doing it in the right way?
Here is my method:
private void getCoordinates() {
getCurrentLocation();
boolean gpsEnabled = lManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gpsEnabled && !netEnabled && location != null) {
Builder alarm = new AlertDialog.Builder(this);
alarm.setTitle("Error");
alarm.setMessage("Localization OFF").create().show();
} else if (location != null && (gpsEnabled || netEnabled)) {
lManager.removeUpdates(lListener);
lat = String.valueOf(location.getLatitude());
lon = String.valueOf(location.getLongitude());
} else {
Builder alarm = new AlertDialog.Builder(this);
alarm.setTitle("Error");
alarm.setMessage("Localization OFF").create().show();
}
}
private void getCurrentLocation() {
lManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location loc) {
location = loc;
}
};
LocationProvider gpsProvider = lManager.getProvider(LocationManager.GPS_PROVIDER);
if (gpsProvider != null) {
boolean gpsEnabled = lManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gpsEnabled && netEnabled) {
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, lListener);
location = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} else if (gpsEnabled) {
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
location = lManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
} else {
Builder alarm = new AlertDialog.Builder(this);
alarm.setTitle("Error");
alarm.setMessage("GPS provider not supported").create().show();
}
}
Please fix it if you can see anything wrong or give me any suggestions.
The problem is when you setup a listener for the location it does not receive a location instantly- especially with a GPS location. So your location is not returning the current coordinates.
You can call the getLastKnownLocation in your location manager but this could be an old or non-existant location. The other option is to call something from
#Override
public void onLocationChanged(Location loc) {
someCallBack(loc);
}
Have a look at my NoteLocation example on github
Link
So I am getting the longitude and latitude as:
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
longitude=location.getLongitude();
latitude=location.getLatitude();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//Or use LocationManager.GPS_PROVIDER
String locationProvider = LocationManager.NETWORK_PROVIDER;
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if(lastKnownLocation!=null){
longitude=lastKnownLocation.getLongitude();
latitude=lastKnownLocation.getLatitude();
}
then I am getting my location depending on these info:
Geocoder myLocation = new Geocoder(Time.this, Locale.getDefault());
List<Address> myList=null;
try {
myList = myLocation.getFromLocation(latitude,longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(myList != null && myList.size()>0) {
address= (Address) myList.get(0);
if(address.getAddressLine(0)!=null){
addressStr += address.getAddressLine(0);
}
if(address.getAddressLine(1)!=null){
addressStr += ", "+address.getAddressLine(1);
}
if(address.getAddressLine(2)!=null){
addressStr += ", " +address.getAddressLine(2);
}
}
But sometimes the location stays null until I restart my phone why that's happening? and is there a way to fix it?
Try to setup your Location Listener as below :
public class BasicMapActivity_new2 extends Activity implements
LocationListener {
private LocationManager locationManager;
private String provider;
Double Latitude, longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
Toast.makeText(BasicMapActivity_new2.this, "GPS signal not found",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if (!enabledWiFi) {
Toast.makeText(BasicMapActivity_new2.this,
"Network signal not found", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
} else {
// do something
}
}
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
Location old_one;
#Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
// Toast.makeText(BasicMapActivity_new.this, "Location " + lat+","+lng,
// Toast.LENGTH_LONG).show();
LatLng coordinate = new LatLng(lat, lng);
Latitude = lat;
longitude = lng;
Toast.makeText(BasicMapActivity_new2.this,
"Location " + coordinate.latitude + "," + coordinate.longitude,
Toast.LENGTH_LONG).show();
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
And do not forget to add permission into manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Update: This is because to add requestLocationUpdates() into onResume() and removeUpdates(this); into onPause(). This way your app will stop updated locations when it is not active. add below into your Activity:
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
this is a documented issue in google forums. Check this thread:
https://code.google.com/p/android/issues/detail?id=57707
Also i think the solution for this is to use Google Location API, this requires that you have Google Play services up to date and >= 2.2 i think. Hope this helps you. I battled this issue for long
This is my last question about gps
Getting 0.0 for latitude and longitude while showing current location in map
Now here is the code I'm using to get the user's current location.
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = mlocManager.getBestProvider(crta, true);
Location loc = null;
if (provider != null) {
loc = mlocManager.getLastKnownLocation(provider);
}
LocationListener mlocListener = new MyLocationListener();
mlocListener.onLocationChanged(loc);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 10, mlocListener);
public class MyLocationListener implements LocationListener{
public MyLocationListener() {
}
#Override
public void onLocationChanged(Location loc) {
if (null != loc) {
String Text = "Your current location is: \n" + "Latitude = \n"
+ loc.getLatitude() + "\nLongitude = \n" + loc.getLongitude();
Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
GeoPoint myGeoPoint = new GeoPoint((int)(loc.getLatitude()*1E6),(int)(loc.getLongitude()*1E6));
mpc.animateTo(myGeoPoint);
mpc.setZoom(10);
objMapView.invalidate();
}
}
#Override
public void onProviderDisabled(String provider){
Toast.makeText(getApplicationContext(), "gps disabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "gps enabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Now the problem I'm facing is, it is not showing me the current location when the gps is turned on. The loc object , loc = mlocManager.getLastKnownLocation(provider); always returns null. I got the value for provider as gps.
But if I turn of my gps connection, the same loc object will have relevant information and it works partiall correct. That means, it gives me the nearest location. I mean the full city location where I am sitting.
But if I on my gps connection, it does not give me even the city location also, does not enters if loop only inside location listener class. I am not getting what is going wrong here.
Any one can tell me how to solve it?
Update:
This is the value I get for loc object if my gps is off
Location[mProvider=network,mTime=1331718353322,mLatitude=12.9053401,mLongitude=74.8359128,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=36.0,mExtras=Bundle[mParcelledData.dataSize=148]].
But if the gps is on, loca return null
CustomLocationManager.Java
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class CustomLocationManager {
private LocationManager mLocationManager;
private LocationValue locationValue;
private Location networkLocation = null;
private Location gpsLocation = null;
private Timer mTimer;
private boolean isGpsEnabled = false;
private boolean isNetworkEnabled = false;
private static CustomLocationManager _instance;
private CustomLocationManager() {}
public static CustomLocationManager getCustomLocationManager() {
if (_instance == null) {
_instance = new CustomLocationManager();
}
return _instance;
}
public LocationManager getLocationManager(Context context) {
if (mLocationManager == null)
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return mLocationManager;
}
public boolean getCurrentLocation(Context context, LocationValue result) {
locationValue = result;
if (mLocationManager == null)
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {}
try {
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {}
if (!isGpsEnabled && !isNetworkEnabled)
return false;
if (isGpsEnabled)
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
if (isNetworkEnabled)
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);
mTimer = new Timer();
mTimer.schedule(new GetLastKnownLocation(), 20000);
return true;
}
LocationListener gpsLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
mTimer.cancel();
locationValue.getCurrentLocation(location);
mLocationManager.removeUpdates(this);
mLocationManager.removeUpdates(networkLocationListener);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
private LocationListener networkLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
mTimer.cancel();
locationValue.getCurrentLocation(location);
mLocationManager.removeUpdates(this);
mLocationManager.removeUpdates(gpsLocationListener);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
private class GetLastKnownLocation extends TimerTask {
CurrentLocationHandler handler;
GetLastKnownLocation() {
handler = new CurrentLocationHandler();
}
#Override
public void run() {
mLocationManager.removeUpdates(gpsLocationListener);
mLocationManager.removeUpdates(networkLocationListener);
if (isGpsEnabled)
gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (isNetworkEnabled)
networkLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
handler.sendEmptyMessage(0);
}
}
private class CurrentLocationHandler extends Handler {
#Override
public final void handleMessage(Message msg) {
if (gpsLocation != null && networkLocation != null) {
if (gpsLocation.getTime() > networkLocation.getTime())
locationValue.getCurrentLocation(gpsLocation);
else
locationValue.getCurrentLocation(networkLocation);
return;
}
if (gpsLocation != null) {
locationValue.getCurrentLocation(gpsLocation);
return;
}
if (networkLocation != null) {
locationValue.getCurrentLocation(networkLocation);
return;
}
locationValue.getCurrentLocation(null);
}
}
}
LocationValue.Java
import android.location.Location;
public abstract class LocationValue {
public abstract void getCurrentLocation(Location location);
}
YourActivity.Java
private void getCurrentLocation() {
CustomLocationManager.getCustomLocationManager().getCurrentLocation(this, locationValue);
}
public LocationValue locationValue = new LocationValue() {
#Override
public void getCurrentLocation(Location location) {
// You will get location here if the GPS is enabled
if(location != null) {
Log.d("LOCATION", location.getLatitude() + ", " + location.getLongitude());
}
}
};
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
First of all I'm not familiar with the Android location API, but did you try the GPS outside? Since GPS doesn't work inside buildings, it's pretty hard to retrieve the location of your mobile device using only your GPS. When indoors, typically your "GPS" position is determined using your WiFi acces point connection instead.