I have a Location Manager:
private LocationManager lm;
I initialize it like this:
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Then i need last known location which i achieve this way:
if(gpsEnabled)
{
lastKnownLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
else if(networkEnabled)
{
lastKnownLoc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
Now i just want the gps icon in the status bar to disappear, above all, i want it to stop checking my position.
Since i never ask for an update, nor i have a LocationListener in my code i don't know how to do it.
getLastKnownLocation() doesn't turn on the Gps, and no ongoing process is started from that call.
As for the GPS status bar icon, it's controlled by the Android system to inform the user that his location has just been obtained, and you can't hide it.
Related
Is it possible to get the current location of user without using GPS or the internet? I mean with the help of mobile network provider.
What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.
http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.
After you get done with most of the code in OnCreate(), add this:
// 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);
You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity.
By getting the getLastKnownLocation you do not actually initiate a fix yourself.
Be aware that this could start the provider, but if the user has ever gotten a location before, I don't think it will. The docs aren't really too clear on this.
According to the docs getLastKnownLocation:
Returns a Location indicating the data from the last known location
fix obtained from the given provider. This can be done without
starting the provider.
Here is a quick snippet:
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import java.util.List;
public class UtilLocation {
public static Location getLastKnownLoaction(boolean enabledProvidersOnly, Context context){
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location utilLocation = null;
List<String> providers = manager.getProviders(enabledProvidersOnly);
for(String provider : providers){
utilLocation = manager.getLastKnownLocation(provider);
if(utilLocation != null) return utilLocation;
}
return null;
}
}
You also have to add new permission to AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
No, you cannot currently get location without using GPS or internet.
Location techniques based on WiFi, Cellular, or Bluetooth work with the help of a large database that is constantly being updated. A device scans for transmitter IDs and then sends these in a query through the internet to a service such as Google, Apple, or Skyhook. That service responds with a location based on previous wireless surveys from known locations. Without internet access, you have to have a local copy of such a database and keep this up to date. For global usage, this is very impractical.
Theoretically, a mobile provider could provide local data service only but no access to the internet, and then answer location queries from mobile devices. Mobile providers don't do this; no one wants to pay for this kind of restricted data access. If you have data service through your mobile provider, then you have internet access.
In short, using LocationManager.NETWORK_PROVIDER or android.hardware.location.network to get location requires use of the internet.
Using the last known position requires you to have had GPS or internet access very recently. If you just had internet, presumably you can adjust your position or settings to get internet again. If your device has not had GPS or internet access, the last known position feature will not help you.
Without GPS or internet, you could:
Take pictures of the night sky and use the current time to estimate your location based on a star chart. This would probably require additional equipment to ensure that the angles for your pictures are correctly measured.
Use an accelerometer to track location starting from a known position. The accumulation of error in this kind of approach makes it impractical for most situations.
boolean gps_enabled = false;
boolean network_enabled = false;
LocationManager lm = (LocationManager) mCtx
.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location net_loc = null, gps_loc = null, finalLoc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (gps_loc != null && net_loc != null) {
//smaller the number more accurate result will
if (gps_loc.getAccuracy() > net_loc.getAccuracy())
finalLoc = net_loc;
else
finalLoc = gps_loc;
// I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy)
} else {
if (gps_loc != null) {
finalLoc = gps_loc;
} else if (net_loc != null) {
finalLoc = net_loc;
}
}
Here possible to get the User current location Without the use of GPS and Network Provider.
1 . Convert cellLocation to real location (Latitude and Longitude), using "http://www.google.com/glm/mmap"
2.Click Here For Your Reference
Have you take a look Google Maps Geolocation Api? Google Map Geolocation
This is simple RestApi, you just need POST a request, the the service will return a location with accuracy in meters.
It appears that it is possible to track a smart phone without using GPS.
Sources:
Primary: "PinMe: Tracking a Smartphone User around the World"
Secondary: "How to Track a Cellphone Without GPS—or Consent"
I have not yet found a link to the team's final code. When I do I will post, if another has not done so.
You can use TelephonyManager to do that .
I have successfully implemented the android location example http://developer.android.com/training/location/retrieve-current.html
I can request location updates via button click and the onLocationChanged method will be triggered to update a map view with the current location.
But consider following problem. When the phones location does not change, onLocationChange will not be triggered anymore. When the user touches the map view and swipes to another location manually, another location request will not trigger onLocationChanged and the map view will not get the current location because the phones position has not changed.
My question is, how can I receive a location in onLocationChanged at 0 location difference? I am using LocationClient and LocationRequest classes and not LocationManager, so this wont work:
manager.requestLocationUpdates(best, 10000, 1, locationListener);
Any ideas?
use getLastLocation() mehod of locationClient it will give you last known location, however it may be null sometime, I observe in some devices it is returning null when I restart the device and call that function before starting gps or location client method.
in that case you can invoke getLastKnownLocation but you have to detect that state, simply try to wrap your class that provide you location and once time it is update with R
requestLocationUpdateslistening on updates and once when it is not triggered it invoke getLastKnownLocation
so simply you can wrap it with some class that contains lastLocation or best location field
public class MyLocationProvider implements LocalisationListener{
private Location bestLocation = null;
private LocationManager locationManager;
//constructor and all that stuff...
public Location getBestLocation(){
if(bestLocation == null)
return locationManager.getLastKnownLocation();
else
return bestLocation;
}
//locationListener methods... that should save their result in `bestLocation` field.
}
This is simple draft of my idea.... I didn't compile it ;)
http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)
I have the following code in my Service:
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider =
locationManager.getProvider(LocationManager.GPS_PROVIDER).getName();
Location location = locationManager.getLastKnownLocation(provider);
while(true)
{
if(...)//every 5 seconds it gets into
{
....//control if the location is not null
lat = location.getLatitude();
lon = location.getLongitude();
alt = location.getAltitude();
Log.i(TAG, "Latitude: "+lat+"\nLongitude: "+lon+"\nAltitude: "+alt);
}
else {
Log.i(TAG, "Error!");
}
}
This code kind of works in my emulator (GPS are inserted into the Log), but in my Mobile device, this code gets to the else branch. Could somebody tell me where is the problem? In my code or in my Mobile device? Thanks in advance.
P.S.: The GPS is turned on, in another apps it works.
getLastKnownLocation() will not fetch subsequent location from the GPS provider. It will return (as the name may suggest) the last known location requested by some code. I assume that you check location being not null in the condition, which is not shown in your code. The location is null if the device "decided" that the last known location is too old or unreliable by other means. You need to request location updates and provide a location listener to get locations repeatetly.
There are lots of tutorials available. Here ist one. of them.
Actually my problem is sometimes i can get the location and sometimes i can't get the location on Android real device. I used following code for getting the location,
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
I need to get the location when my application starts up. I don't know why sometimes it fails. How to resolve this issue?
#jsmith commented on my quesion,
Increase your power requirements and make sure you test where you can
get a network or gps signal. If that helps, then it's simply a case of
the sensors not getting enough reception to know the location.
This is the right answer for my question. Now its working fine.
You forgot to request for 'Location Updates', use this function after declaring the LocationManager:
locationManager.requestLocationUpdates(provider, 0, 0, this);
and you have to let the class implements LocationListener then add this function:
public void onLocationChanged(Location location) {
//get the location here
}
If you don't want to get the location repeatedly, after you get the location you have to stop the 'Location Updates' this way:
locationManager.removeUpdates(this);
in case you want the application always listen for the 'Location Updates', you better request for updates in onResume() and remove the updates in onPause() & onDestroy()
Good Luck
So I'm trying to sample the gps coordinates just once in an application. I don't want to create a LocationListener object to constantly get gps updates. I want to wait until receiving the coordinates, and then proceed on to another task.
Here is a code snippet
LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);
The loc variable is always null in the emulator. I tried using the command "geo fix latitude longitude" to set it, and also I tried using the DDMS way of setting it. Neither method had any effect on the code. Also the snippet isn't causing any exceptions.
Thanks for your help.
The call to request update for a location is not blocking, hence it wont wait there. Also the provider in emulator may not have been started.
A possible check could be to see if the settings in it disable gps provider ? then send geo fix.
However, I would use Location Listener, it would be ideal in your case since you need a geo fix to proceed further.Location Listener is Used for receiving notifications from the LocationManager when the location has changed. You can unregister the listener after first geofix.
Note: It can take some time on device to get current location, and even on device this can return null.
Try using the MyLocationOverlay , create a runnable that does what you need to do with that GPS location, and pass it to
boolean runOnFirstFix(java.lang.Runnable runnable)
Queues a runnable to be executed as soon as we have a location fix.
and then disable the location updates for the MyLocationOverlay.
Edit: The reason the location is null is because at the time that code is run, no geofix has been received.