Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location Manager's requestLocationUpdates called only once

I am calling the method and expect location updates multiple times:

locationManager.requestLocationUpdates("gps",0 ,0, loc_listener);

My loc_listener is defined as:

LocationListener loc_listener = new LocationListener() {
    private final String TAG = "xoxoxo.LocationListener";

    public void onLocationChanged(Location l) {
        Intent locationAlert = new Intent("xoxoxo.LOCATION_CHANGED")
                .putExtra("target_location", l);
        sendBroadcast(locationAlert);
        // locationManager.requestLocationUpdates("gps", 0 ,0, this);
    }

    public void onProviderEnabled(String p) {
        Log.i(TAG, "Provider enabled");
    }

    public void onProviderDisabled(String p) {
        Log.i(TAG, "Provider disabled");
    }

    public void onStatusChanged(String p, int status, Bundle extras) {
        Log.i(TAG, "Status changed");
    }
};

Defined as is, I will only get an update once, both on HTC Evo 2.2 and 2.2 + Google API emulator. The hack to get multiple updates is to uncomment the line which registers for updates on each update:

locationManager.requestLocationUpdates("gps", 0 ,0, this);

Have you guys seen anything like this?

like image 453
Denis Masyukov Avatar asked Jan 23 '11 07:01

Denis Masyukov


People also ask

How will you instantly get location manager object?

First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to getLastKnownLocation() as an argument to get the location.

What is the use of location manager?

LocationManager is the main class through which your application can access location services on Android. Similar to other system services, a reference can be obtained from calling the getSystemService() method.

What is the use of location manager in Android?

These services allow applications to obtain periodic updates of the device's geographical location, or to be notified when the device enters the proximity of a given geographical location.

Which method is invoked when a new location is available?

Once you have Location object, you can use Geocoder. getFromLocation() method to get an address for a given latitude and longitude. This method is synchronous, and may take a long time to do its work, so you should call the method from the doInBackground() method of an AsyncTask class.


1 Answers

I have never seen the issue. The following is my code to test LocationManager and LocationListener. It works as expected when LocationListener is implemented as an anonymous class.

package com.test.locationmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationManagerStatus extends Activity {

    private LocationManager locationManager;
    private TextView textView;
    private final LocationListener gpsLocationListener =new LocationListener(){

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            final String tvTxt = textView.getText().toString();
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(tvTxt + "GPS available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(tvTxt + "GPS out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(tvTxt + "GPS temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "GPS Provider Enabled\n");
        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "GPS Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            locationManager.removeUpdates(networkLocationListener);
            textView.setText(textView.getText().toString()
                    + "New GPS location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");
        }
    };
    private final LocationListener networkLocationListener =
                                                        new LocationListener(){

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras){
            final String tvTxt = textView.getText().toString();
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(tvTxt + "Network location available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(tvTxt + "Network location out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(tvTxt
                        + "Network location temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Enabled\n");
        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            textView.setText(textView.getText().toString()
                    + "New network location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textview);
        locationManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 5000, 0,
                networkLocationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000, 0, gpsLocationListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(networkLocationListener);
        locationManager.removeUpdates(gpsLocationListener);
    }
}
like image 50
Don Avatar answered Nov 15 '22 20:11

Don