I am listening to 2 location providers
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500,
5,this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
500, 5, this);
it is possible somehow remove one listener? For example for one task I just want to use GPS update, and while doing this task I want that NETWORK_PROVIDER will be disabled. It is possible somehow? I tried to do like this :
locationManager.removeUpdates(this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5,this);
But this is not really good, because if GPS had fix, this method is removing it and GPS starts to search satellites again.
You can create two different LocationListener:
private LocationListener gpsListener = new LocationListener() {
#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
}
#Override
public void onLocationChanged(Location location) {
if(location != null){
// do your work for gps location
}
}
};
private LocationListener networkListener = .... // same init as gpsListener
And use like:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5, gpsListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 5, networkListener);
Then you can use the approach you are doing, without losing GPS fix.
Instead of:
locationManager.removeUpdates(this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5,this);
You will use:
locationManager.removeUpdates(networkListener);
Related
I am trying to fetch my device's location for an application. The code returns 0.0 for latitude and longitude. What do I need to change? Stack Overflow wouldn't let me post this question without another sentence. Hopefully, this much text is enough.
//Getting the location
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location !=null){
plat = location.getLatitude();
plong = location.getLongitude();
}
}
#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
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
Log.d("lat: ",Double.toString(plat));
Log.d("long: ",Double.toString(plong));
RequestLocationUpdates is asynchronous- it takes time. The value of plat and plong will not be ready until onLocationChanged is called. SO you can't use them until then.
move
Log.d("lat: ",Double.toString(plat));
Log.d("long: ",Double.toString(plong));
to
public void onLocationChanged(Location location)
and make sure that GPS is enabled
I am working on a widget for Android, which displays your current location. To get the current latitude and longitude I have the following code:
public class LocationInfo extends Activity {
RemoteViews remoteViews;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myLocationListener locationListener = new myLocationListener();
LocationProvider locationProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
class myLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if(location != null)
{
double Long = location.getLongitude();
double Lat = location.getLatitude();
remoteViews.setTextViewText(R.id.widget_textview3, Double.toString(Long));
}
else {
remoteViews.setTextViewText(R.id.widget_textview3, "LOADING...");
}
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
However, I get an error at the arguments of requestLocationUpdates. It says "Syntax error on tokens, VariableDeclarator expected instead" at 0,0, locationListener.
Also, this is a separate class. How would I then run this in my widget? Something like:
LocationInfo locationProvider = new LocationInfo();
LocationProvider.run();
maybe? Again, any help would really be appreciated!
You are not allowed to execute
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
outside of methods.
Just move in to, for example, onCreate of Activity.
Good luck
i'm new in java/android app and i'm creating an app that uses user-location. I want the location to be updated at the begining of the app.
The problem is that my location class is an activity and i don't want to show another contentview for this class.
Actually, i want the location thing to be done in background, without changing the UI, in a separated class.
Is it possible? How?
Thanks :P
There is no need to put the location in a different activity, the LocationManager already does it in the background:
public void getLocation(){
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = new LocationListener() {
#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
}
#Override
public void onLocationChanged(Location location) {
//do something with the new location
if (location != null)
gpsLocation = location;
}
};
gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, gpsLocationListener);
}
Using the LocationManager you should be able to use what ever kind of activity (or service) you want.
I have implemented the Demo to show the current lat-long of the User.
Now I am able to see the lat-long of the current Position.
but I want to Set it to to be displayed in every 1 min of interval.
The code is as below:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener lListener = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
}
private class mylocationlistener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(MainActivity.this,
"My current location:\nLatitude:"+location.getLatitude() + "\nLogitude:" + location.getLongitude(),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
}
}
}
So, What should I have to do to make it to display the current lat-long in every 1 min of interval?
I think I have to use the thread to implement it. How to make it possible?
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
The argument in the method signify - provider, time, distance and the location listener.
Therefore to request an update from the Location listener you need to set the second parameter to 1000*60, because the time is recorded in milliseconds.
Hello I've been using this guide to determine the locationof the user: http://developer.android.com/guide/topics/location/obtaining-user-location.html
and my current code looks like this:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000);
mapController.animateTo(test);
}
#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
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
I only get error and the app shuts down though when I'm using this activity.
Thankful for help
PLease confirm that you have alloted the following permission to your manifest file
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
I guess you are executing this code in the device
try this on device
Setting -> Location -> Use GPS
AND
Setting -> Application Settings -> Development -> Allow Mock Location
I solved it, I didn't instantiate mapContoller :>. But I've another problem now. which I will post in another question.