Lets say that i have an object that asynchronously will be seted (an user GPS position for example).
Is there a way to only call a method in an Activity when this object is not null?
If you simply want to call a method if some object is not null then:
if (myObject != null) { callMyActivityMethod()}
Based on your updated information, you only want to call the method once you have received a location from the GPS.
A LocationListener is used for receiving notifications from your LocationManager.
Create a LocationListener and pass it in your requestForLocationUpdates(). It will then notify you when a new location has been received. For Example:
LocationListener locListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callMyActivityMethod();
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
...
requestLocationUpdates(provider, minTime, minDistance, locListener);
...
Related
I would like to locate the user by clicking on a button. Therefore I use this code:
lm = getSystemService(LocationManager.class);
provider = LocationManager.GPS_PROVIDER;
listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Log.e("UPDATE","UPDATING LOCATION");
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
The requestLocationUpdates method is called but the onLocationChanged method is only called a few times. GPS is activated and the permissions are granted.
Do you have any idea why onLocationChanged is not always called? I also checked getLastKnownLocation which is sometimes false after calling requestLocationUpdates.
I think onLocationChanged() is not suppose to be called always, it will only be called if a location changes.
I changed the request method to lm.requestSingleUpdate(criteria, listener,null); which works perfect as well as requestLocationUpdates with criteria.
Seems to be a problem with the provider.
Its not clear to me how to use the location listener.
Do I do this:
public Location actualLocation;
private class mLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
actualLocation = location;
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
//Middle of code
currentLatitude = actualLocation.getLatitude()
currentLongitude = actualLocation.getLongitude()
or this:
public Location actualLocation;
private class mLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
//Middle of code
actualLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
currentLatitude = actualLocation.getLatitude()
currentLongitude = actualLocation.getLongitude()
There are variables used that were not declared in this snippets of code. It's on purpose. I shortened the code for the sake of simplicity.
UPDATE:
My question was: ¿Do I use getLastKnow location or do I manually save the location every run be it updates? ¿Would that work the same?
You need to call LocationManager.registerLocationUpdates. You pass the LocationListener into that call, and the framework will call those functions whenever it has a new location.
all
I have carefully read many related Q&A in this site, but nothing help me. Here is my code, it works fine with GPS on, but went wrong once i turn off the GPS.
/**
*
* #author Jimmy Chiang
*
*/
public class GetLocationTask extends AsyncTask<Context, Void, Location> {
private OnLocationListener[] mGpsListeners;
private Location location;
private LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location lo) {
location = lo;
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
public interface OnLocationListener {
public void onLocationGot(Location location);
}
public GetLocationTask setOnLocationListener(
OnLocationListener... listeners) {
mGpsListeners = listeners;
return this;
}
#Override
protected Location doInBackground(Context... params) {
LocationManager locationManager = (LocationManager) params[0]
.getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.NETWORK_PROVIDER;
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
}
location = locationManager.getLastKnownLocation(provider);
while (location == null) {
locationManager.requestLocationUpdates(provider, 1000, 1,locationListener);
}
return location;
}
#Override
protected void onPostExecute(Location location) {
for (OnLocationListener listener : mGpsListeners) {
listener.onLocationGot(location);
}
}
}
The exception shows:
can't create handler inside thread that has not called
looper.prepare()
Some posts show that the problem should be call someting in UI Thread, but i can't find any of this. Thanks so much if you know what's going on~
change
locationManager.requestLocationUpdates(provider, 1000, 1,locationListener);
with
locationManager.requestLocationUpdates(provider, 1000, 1,locationListener, Looper.getMainLooper());
as stand in the docs
Looper.getMainLooper()
Returns the application's main looper, which lives in the main thread
of the
application.
the looper arguments of requestLocationUpdate is a Looper object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread.
If you want the AsyncTask to be the one that will receive the messages you have to use
Looper.myLooper().prepare();
locationManager.requestLocationUpdates(provider, 1000, 1,locationListener, Looper.myLooper());
I'm trying to get the devices location by getting all listeners:
LocationManager locationManager = (LocationManager) myContext.getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
for (String s : locationManager.getAllProviders()) {
locationManager.requestLocationUpdates(s, checkInterval,
minDistance, new LocationListener() {
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
// if this is a gps location, we can use it
if (location.getProvider().equals(
LocationManager.GPS_PROVIDER)) {
doLocationUpdate(location, true);
stopGPS();
}
}
#Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
});
gps_recorder_running = true;
}
// start the gps receiver thread
gpsTimer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
Location location = getBestLocation();
doLocationUpdate(location, false);
if ((System.currentTimeMillis()-startMillis)>maxCheckTime){stopGPS();}
}
}, 0, checkInterval);
}
The problem comes when I want to stop the Listeners. I tried to cancel the timer:
gpsTimer.cancel();
But it doesn't stop the Listeners. I think I have to use locationManager.removeUpdates, but how do I stop all Listeners?
Thanks
You must keep a list of all the location listeners you register and then call unregister with each of them when you are done. Either that or just reuse the same listener for each call and then unregister it once.
Edit
//Make the following line a field in your class
List<LocationListener> myListeners = new ArrayList<LocationListener>();
for (String s : locationManager.getAllProviders()) {
LocationListener listener = new LocationListener() { .... }; //I'm cutting out the implementation here
myListeners.add(listener);
locationManager.requestLocationUpdates(s, checkInterval,
minDistance, listener);
}
I am trying to get the location in my Android app as follows
LocationManager lmanager = (LocationManager)context.GetSystemService(Context.LocationService);
if (lmanager.IsProviderEnabled(LocationManager.NetworkProvider))
{
Location currentLoc = lmanager.GetLastKnownLocation(LocationManager.NetworkProvider);
}
I tried with LocationManager.gpsProvider also instead of the Network provider.
In all the cases I am getting a null reference exception for currentLoc.
The GetLastKnownLocation() method returns the last location it knew about, which could be null if it has never received any updates before. I think what you want to do here is listen for location updates, and then respond to them once they come in. You can do this by implementing the ILocationListener interface:
class MyLocationListener : Java.Lang.Object, ILocationListener
{
public void OnLocationChanged(Location location)
{
}
public void OnProviderDisabled(string provider)
{
}
public void OnProviderEnabled(string provider)
{
}
public void OnStatusChanged(string provider, Availability status, Bundle extras)
{
}
}
In your activity you can declare that you'd like to use this class to start listening for updates like this:
var locationManager = (LocationManager)GetSystemService(LocationService);
var locationListener = new MyLocationListener();
locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 5000, 2, locationListener);