2

I would like to know if its possible to get the users location without the use of a LocationListener.

The reason why i ask is that my locationListener events are not being called.

public class LocationActivity extends Activity implements LocationListener{

    private LocationManager m_locationManager;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        LinearLayout content = new LinearLayout(this);
        content.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        content.setOrientation(LinearLayout.VERTICAL);
        content.setBackgroundColor(Color.TRANSPARENT);

        TextView infoLabel = new TextView(this);
        infoLabel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        infoLabel.setTextSize(20.0f);
        infoLabel.setText("Initializing...");
        content.addView(infoLabel);

        try{
            m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            String provider = null;

            if ( m_locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                provider = LocationManager.GPS_PROVIDER ;
                Log.d("Unity", "Using GPS");
                m_locationManager.requestLocationUpdates(provider, 0, 0, this);
            } else if(m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                provider = LocationManager.NETWORK_PROVIDER;
                Log.d("Unity", "Using Netword");
                m_locationManager.requestLocationUpdates(provider, 0, 0, this);
            } else {
                Log.d("Unity", "Provider Not available");
            }

        }catch(Exception ex){
            Log.d("Unity", "locatons error " + ex.getMessage());
        }

        setContentView(content);

    }

    public void onLocationChanged(Location location) {
        Log.d("Unity", "UserLocation Lat:" + location.getLatitude() + " Long:" + location.getLongitude());
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {Log.d("Unity", "onStatusChanged");}
    public void onProviderEnabled(String provider) {Log.d("Unity", "onProviderEnabled");}
    public void onProviderDisabled(String provider) {Log.d("Unity", "onProviderDisabled");}
} 

See this post for more info:

Android: Can't create handler inside thread that has not called Looper.prepare()

Modified to reflect mmeyer comments

Community
  • 1
  • 1
user346443
  • 4,672
  • 15
  • 57
  • 80

1 Answers1

1

First, you can check the last known location when you select the provider with LocationManager.getLastKnownLocation(provider) and see if it's not null and not too old.

Beyond that your code looks adequate. I suspect the issue might be that the 3rd param in your call to requestLocationUpdates says to only send updates if the location has changed by more than 100 meters. For most situations where youre running in debug and watching logcat, moving a hundred meters a second seems unlikely.

From the API docs:

The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

mmeyer
  • 3,598
  • 1
  • 19
  • 22
  • So getLastKnownLocation is not null but still no updates. Are you aware of any other way i can get the current position with out using locationListener? Or is it the only way it can be obtained? – user346443 Sep 03 '11 at 19:42
  • No, I dont think there is another way. getLastKnownLocation just gives you a fast way to get a location before you get any update, so the UI/user doesnt have to wait. But, I do still think the reason youre not getting updates is because your params ask for updates only when location has changed by more than 100 meters. What happens when you change the 3rd param to 0? – mmeyer Sep 03 '11 at 19:46
  • Modifying both parameters to 0 does nothing and getLastKnownLocation is over 24 hours old. Im calling this from the Unity Game engine. So its obviously screwing the the events some how. The view is open correctly it just those events are not firing. Thanks for your help with this. – user346443 Sep 03 '11 at 19:55
  • 1
    Hmmm, I cant see how unity would muck with this given that it's still and Android Activity. I have code that does pretty much the same thing with no issues. The only real difference I see is that I ask for location updates from the onResume phase rather than onCreate...cant see how that would matter, but worth a quick hack to see. – mmeyer Sep 03 '11 at 20:17