In Android if LocationServices is disabled, is there any way that I can get the current location through wifi/network. I know there is one way of showing the user a popup to change his LocationSettings , but I don't want to show the popup.
So is there any way of getting the current location even if LocationServices is disabled ?
use LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER
The entire code and how to use is given here :
http://developer.android.com/guide/topics/location/strategies.html
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Yes of course, you can use:
// let Android select the right location provider for you
String myProvider = locationManager.getBestProvider(myCriteria, true);
check this for more details good-way-of-getting-the-users-location-in-android and this Location Strategies
Related
I don't want to use service which is continuously running in background ! Actually in my project user will send SMS command from any smartphone to his/her misplaced smartphone.My app will detect that particular "SMS command" and in return it will send the current location of misplaced mobile.
can it be done through intent service ? I m damn confused ... Its single time operation how to perform it efficiently ... ?
by using NETWORK_PROVIDER getting geolocation
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
and add this permission in manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
How can I get continuous geo-location of android device programmatically ?
I'm having troubles of getting position coordinates continuously of android device and show the navigation move according to the geolocation.
According to Android documentation :
// Acquire a reference to the system Location Manager LocationManager locationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {} };
// Register the listener with the Location Manager to receive location
updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
> 0, 0, locationListener);
More information available on their page : http://developer.android.com/guide/topics/location/strategies.html
I'm trying to get the user's Location by showing that he is in which area with Particular location in google maps with the help of Android API while surfing in internet I came to know that getLocationManger() is used to view the Location but it shows only by Latitude,Longitude.
If someone have any idea about this please help me friends.
You can get the user's current location in terms of latitude and longitude and simply make an Implicit Intent to launch map with these values.
To get current location:
LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
//Toast.makeText(getApplicationContext(),"Entered onLocationChanged",Toast.LENGTH_SHORT).show();
Log.d("LIFECYCLE","Entered onLocationChanged");
lat = location.getLatitude();
lon = location.getLongitude();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
To show on google map:
Intent intent = null,chooser = null;
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:"+lat+","+lon));
chooser = Intent.createChooser(intent,"Launch Maps");
startActivity(chooser);
/* creating chooser because:
If this is not done, your app may crash when run on the emulator because the emulator may not have an activity installed which can handle the intent.*/
Try this one
LocationManager lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Location net_loc = null;
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
you can get the current location from net_loc.
I am getting longitude=0.0 and latitude=0.0 on tablet while this is working perfectly on the phone.
I am using LocationManager.NETWORK_PROVIDER and not the GPS_PROVIDER so what could be the cause please?
Logcat output??
that's my code:
// Acquire a reference to the system Location Manager
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();
}
Check out Location settings inside your device settings. This seems not a problem with your code, but your device settings. In my Ginger bread, it's as:
Settings -> Location&security -> Use wireless networks
I have tried to do my research about GPS issues before posting here. When I tested my code It repeats the same output over and over. The method gpsStart() is called on a timer. Fine and Coarse location permissions have been added to the manifest. The method appendLog() stores the output in a file.
public void gpsStart() {
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Toast.makeText(getApplicationContext(), "location changed",
// Toast.LENGTH_SHORT).show();
// Called when a new location is found by the network location
// provider.
appendLog("Lat: " + location.getLatitude() + "\nLng: "
+ location.getLongitude()+"\n");
text3.setText("Lat: " + location.getLatitude() + "\nLng: "
+ location.getLongitude());
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
// Register the listener with the Location Manager to receive location
// updates
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
You must not call gpsStart() method in the timer. I am going to say how Location Listener works
What is Location Listener?
Location Listener is the class which notify you when your current location will be change. For receive the location updates you will have to register LocationListener class using LocationManager class.
When to register Location Listener?
It depends on the requirement of your application. For example if you want location listener for display current location on map then you should register Location Listener in onCreate() or onResume() method of the activity and unregister receiver in onPause() or onStop() method. If you want to receive the location even if your application is not running then you can use a service to receive location.
How to Register/Unregister Location Listener?
To register the LocationListener you first need the instance of the LocationManager class which you can get using the context like
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
After then you will have to set the location provider. There are 2 types of providers which are often used.
GPS provider
Network Provider
Now to register the location receiver with this location provider there is a method requestLocationUpdates of LocationManager. In this method first argument is the provider name second argument is the minimum time for request location update. Third argument is the minimum distance to request for the location change.Last argument is for locationListener.
Here how you can use the method
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, minTime, minDistance, locationListener);
To unRegister location updates you can use the below method
locationManager.removeUpdates(locationListener)
Note: You are calling a method gpsStart in the timer as you have mentioned in your question so it is adding location listener each time when this method calls.So all the listeners are triggering you the new location so probably you are getting same location multiple times. Instead of it, you should call this method once when your activity starts and unRegister this locationListener when your activity finish.
Hopes you are getting. :D
Enjoy!!!
Better you create a 2 listeners.1 for Gps and other for network.
the following are code samples
public class MyLocationListener extends LocationListener
{
public void onLocationChanged(Location location)
{
// Toast.makeText(getApplicationContext(), "location changed",
// Toast.LENGTH_SHORT).show();
// Called when a new location is found by the network location
// provider.
appendLog("Lat: " + location.getLatitude() + "\nLng: "
+ location.getLongitude()+"\n");
text3.setText("Lat: " + location.getLatitude() + "\nLng: "
+ location.getLongitude());
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
}
MyLocationListener gpsListener=new MyLocationListener();
MyLocationListener networkListener=new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpslocationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networklocationListener);
Thank you very much for your answers. Dharmendra you are correct, I did need to close the LocationListeners, however the answer to my question was much simpler. I needed to change the minimum time (the time the listener waits to get an answer) to a value above zero. The value I needed to change is shown in bold below (MINTIME).
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINTIME, 0, gpslocationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINTIME, 0, networklocationListener);