requestSingleUpdate doesn't work :( - android

I have the following code:
SosServiceListener mlistener = new SosServiceListener(this);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// getting network status
boolean isNetworkEnabled = manager.
isProviderEnabled(LocationManager.NETWORK_PROVIDER);
String provider;
if (isNetworkEnabled) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
provider = LocationManager.GPS_PROVIDER;
}
manager.requestSingleUpdate(provider, mlistener, null);
manager.getLastKnownLocation(provider);
but the SosServiceListener is never called :( any idea why?

String provider;
if (isNetworkEnabled) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
provider = LocationManager.GPS_PROVIDER;
}
is GPS enabled on the device, with Satellite option set in Security section of mobile settings? because you are assuming that GPS_PROVIDER is enabled, just because NETWORK_PROVIDER is disabled? which is not accurate ...
also manager.getLastKnownLocation(provider); will return Location object directly and will not result a call to callback methods in the listenet...
so you need to use either one of them...

Related

Android: GPS Location not working

I am trying to retrieve Location as follows:
LocationManager locationManager = (LocationManager)MyApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
String provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// NOTE: the location below is null !!
Location location = locationManager.getLastKnownLocation(provider);
String lat = location.getLatitude();
String lng = location.getLongitude();
}
The 'location' above is null. Why?
But when i open the (Google) Maps app - it shows location correctly, and even a notification icon (looks like a exclamation mark) shows up.
And there is a GPS Coordinates app - which also shows up blank.
I have turned on Settings > 'Location' on the phone. If it matters this is Android 4.4.4 Sony Experia phone.
Well, it seems that you're not checking properly if the GPS is working or not. The way you should do it:
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if (!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
//GPS is not enabled !!
}
You can also create an AlertDialog so the user is aware that the GPS is not enabled and you can send him to the GPS settings by doing this:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
After this, I'd implement a location listener and get coordinates, here is an example, check it out
How do I get the current GPS location programmatically in Android?
UPDATE: They might be getting coordinates through other provider (usually the one which is working better at that moment). So if the GPS it not working, just try other provider, here is an example:
Location lastKnownLocation = null;
List<String> providers = null;
if(locationManager != null) providers = locationManager.getAllProviders();
if(providers != null)
{
for(int i=0; i<providers.size(); i++)
{
if(locationManager != null) lastKnownLocation = locationManager.getLastKnownLocation(providers.get(i));
if(lastKnownLocation != null)
{
position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
break;
}
}
}

Android LocationManager network provider returns null

I wanted to get my GPS coordinates using Android App. I started developing, and I can get GPS coordinates, but they are not accurate. I wanted to use NETWORK_PROVIDER, but the Location by this provider is always null. More interesting, isProvicerEnabled returns true.
I used example from this thread (best answer)
enter link description here
private void _getLocation() {
// Get the location manager
try {
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
double latitude = -1;
double longitude = -1;
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
if (isNetworkEnabled) {
showToast("network");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
1000,
0, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
else if (isGPSEnabled) {
showToast("gps");
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000,
0, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
showToast("" + latitude + longitude);
} catch (Exception e) {
e.printStackTrace();
}
I have all the permissions in manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
I know the code is dirty, but for now it's only for testing. Do I miss something? I found similar examples in many places, and it seems pretty straight, so I am a little confused.
My phone works ok, GPS and network works fine. For example Google Maps application I have works well. Any suggestions?
Please do NOT use this code. It's bad. It has a lot of errors. Also, getLastKnownLocation will return null if it doesn't have a location yet. Which it always will if nobody on the phone is using requestUpdates.
Your code is taken from a class that was posted on a very old thread on here called GPSTracker. I've been trying to kill that code for months- it causes far more problems than it helps. If you want better example code, try http://gabesechansoftware.com/location-tracking/ which is a blog post I wrote about how bad that code is. It will show you the correct way to do it, and explains some of what's wrong with that code.

What is the simplest way to get the user's longitude and latitude in Android ? Even without using listener?

My app just need to know user's country, and my app doesn't care where user moves.
All I want to know is :
When user start app, app will send user's approximately location(latitude and longitude) to server.
Here is my code with error. I try to send a string to getlocation function, and I hope it can return original string with adding latitude and longitude information.
private String getlocation (String url){
LocationManager status = (LocationManager) (this.getSystemService(Context.LOCATION_SERVICE));
if (status.isProviderEnabled(LocationManager.GPS_PROVIDER) || status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.v("print","enable to locate");
} else {
Log.v("print","fail to locate");
}
Location location = status.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null){
Log.v("print","succed!!!##");
Double longitude = location.getLongitude();
String loc=Double.toString(longitude);
url=url+loc;
}
else{
Log.v("print","location return null");
}
return url;
}
I use network_provider, and it always print "enable to locate", then print "location return null"
So what's wrong with my code that my location always return null.
I use AVD manerger, and I also try GPS_PROVIDER. I use Emulator control to send Decimal on location control. It doesn't work ,too.
By the way, I set network and GPS permission,too.
So much thanks for help!
I am getting location by below code but enable " use wireless networks " in settings i.e ( through settings - Location services - use wireless networks)
public void getCurrentLocation() {
LocationManager locationManager;
String bestProvider;
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
Log.e(TAG, "Latitude: "+location.getLatitude()+" , Longitude: "+location.getLongitude());
}

Android google maps use GPS location NOT network

In my application I want to get the location of the user and show it on a map (google maps).
In the settings of a android device you can check
Use Wireless networks
Use GPS satellites
When the user starts the mapactivity I show a dialog in wich the user can choose to get his current location or not.
Old code deleted
If the user wants to get his location I check if the gps is enabled. If not, I start the settings. Else I try to get the location. But even when GPS is active and "use wireless networks" not he won't get a location. Is there a solution to check if "Use wireless networks" is enabled. Or what part of my code I need to change to get location only by GPS signal?
Thanks
EDIT mycurrentcode:
boolean isGps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetwork = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGps && !isNetwork){
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if(isGps && !isNetwork) {
String bestProvider = LocationManager.GPS_PROVIDER;
Location location = lm.getLastKnownLocation(bestProvider);
Toast.makeText(MapsTabActivitiy.this, "Location niet beschikbaar.", Toast.LENGTH_SHORT).show();
} else if(!isGps && isNetwork) {
String bestProvider = LocationManager.NETWORK_PROVIDER;
Location location = lm.getLastKnownLocation(bestProvider);
if(location == null){
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} else if (location != null){
mapcontroller.animateTo(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)));
mapcontroller.setZoom(15);
}
}
else {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bestProvider);
if(location == null){
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} else if (location != null){
mapcontroller.animateTo(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)));
mapcontroller.setZoom(15);
}
}
So I check with isProviderEnabled for GPS_PROVIDER AND NETWORK_PROVIDER. And with the if, else if I change how to get the location. The only case I need to find is case 2 (else if(isGps && !isNetwork){}). Get the location by GPS signal.
You're getting the right provider using the Criteria class, but you can simple use the LocationManager.GPS_PROVIDER to request the location updates using only GPS. Change
String bestProvider = lm.getBestProvider(criteria, false);
to
String bestProvider = LocationManager.GPS_PROVIDER;
Hope this helps.

criteria for both GPS and network

I would like to know how can I get location from the best provider
do I have to make two separate criteria 1 for the GPS and 1 for the network or is there a way to put them all together ?
this is my code when I add the COARSE criteria the GPS does not go on (no GPS flashing logo on the top of the screen) and when I use the FINE criteria I dont get any thing from the network.......so do I have to write criteria for both and switch between them for what ever is available or can they both be in the same criteria ?
because I have the "getBestProvider(criteria, true);" in my code so it should get the location from the best provider....right..??!
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//// criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
Using a getBestProvider with Criteria.ACCURACY_FINE will never return the NETWORK_PROVIDER, even though wifi position is usually quite accurate.
In my application, I use getProviders to get all providers matching my criteria, then add the NETWORK_PROVIDER if it is activated and not yet in the list.
Then I launch a requestLocationUpdates for each of these providers, making sure to call removeUpdates when I get a location accurate enough.
This way, if the location provided by the network is accurate enough, the gps provider will be turned off
#nicopico getBestProvider with Criteria.ACCURACY_FINE can return the NETWORK_PROVIDER if gps is not enabled
i use the following code in my app
public void getLocation() {
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
// boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
Log.e(TAG, "getLocation fired Google Play Services are not available");
mHandler.post(new UiToastCommunicaton(context.getApplicationContext(),
context.getResources().getString(R.string.gpserv_notfound)));
}
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) context.getSystemService(IntentService.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//check if network location provider context on
Log.e(TAG, "network location provider not enabled");
} else {
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
//criteria.setPowerRequirement(Criteria.POWER_LOW);
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Check provider exists then request for location
if (provider != null) {
requestLocation(locationManager, provider);
} else {
//start wifi, gps, or tell user to do so
}
}
}
public void requestLocation(LocationManager locationManager, String provider) {
Log.e(TAG, "requestLocation fired getting location now");
if (provider != null) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
//locationManager.requestLocationUpdates(provider, 0, 0, this, Looper.getMainLooper());
} else {
//tell user what has happened
}
}
this code is in a class that implements locationListener
you can edit this line
locationManager.requestLocationUpdates(provider, 0, 0, this);
to reflect your implementation of locationListener

Categories

Resources