tags:

views:

7756

answers:

6

Hi

I am trying to get the GPS location of my G1 using the following code

In Activity

MyLocationListener myListener = new MyLocationListener();
LocationManager myManager = (LocationManager)getSystemService(LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, myListener);

This is the LocationListener class

public class MyLocationListener implements LocationListener {

  private static double latitude;
  private static double longitude;

  @Override
  public void onLocationChanged(Location arg0) {
    latitude = arg0.getLatitude();
    longitude = arg0.getLongitude();
  }

  @Override
  public void onProviderDisabled(String provider) {
  }

  @Override
  public void onProviderEnabled(String provider) {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {}

  public static double getLatitude() {
    return latitude;
  }

  public static double getLongitude() {
    return longitude;
  }

}

I waited for as long as 30 seconds but none of the listener methods were called. The GPS icon shows up, stays there for some time but for some reason I don't get a fix, even outdoors in bright sunlight. I am testing on a G1 with 1.5 SDK.

Can someone please tell me what's wrong with the code? Thanks.

Adding log

06-02 18:30:43.143: ERROR/System(52): java.lang.SecurityException
06-02 18:30:43.143: ERROR/System(52):     at android.os.BinderProxy.transact(Native Method)
06-02 18:30:43.143: ERROR/System(52):     at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
06-02 18:30:43.143: ERROR/System(52):     at android.os.ServiceManager.addService(ServiceManager.java:72)
06-02 18:30:43.143: ERROR/System(52):     at com.android.server.ServerThread.run(SystemServer.java:155)
06-02 18:30:43.152: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service

06-02 18:30:43.382: ERROR/SystemServer(52): Failure starting StatusBarService
06-02 18:30:43.382: ERROR/SystemServer(52): java.lang.NullPointerException
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.updateBluetooth(StatusBarPolicy.java:749)
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.<init>(StatusBarPolicy.java:282)
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.installIcons(StatusBarPolicy.java:337)
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.ServerThread.run(SystemServer.java:186)
06-02 18:30:43.382: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service
+1  A: 
  1. Is your GPS enabled in your device settings? I presume so since you are getting the GPS icon, but I figured I'd check.
  2. onProviderDisabled() will only get called if you disable GPS in the settings, and so that code is unlikely to get invoked.
  3. Have you tried a distance of 0 instead of 2000?
  4. Are you sure that your call to requestLocationUpdates() is actually getting executed?
  5. Does your log show any errors? You can examine your log via adb logcat, DDMS, or the DDMS perspective in Eclipse.
  6. Have you tried any existing code, published, that should work? For example, you can grab the source code to my Android book, where the Weather and WeatherPlus samples show the use of LocationManager.
CommonsWare
1. In the settings - yes2. It doesn't, none of the methods are invoked3. tried that. still don't get a fix4. Yes5. Yes I actually am but don't know why. Posting it in the question
lostInTransit
just to add, I have all the necessary permissions in the manifest.xml file - INTERNET, ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION
lostInTransit
3. The second argument isn't distance, it's time.
fiXedd
+5  A: 

Firstly, in your activity, where you're calling requestLocationUpdates(), the second argument is the minimum time, not the scheduled time. You're currently asking for an update AT MOST every 2 seconds. It won't actually report anything until it can... sometimes it takes a little while.

If your GPS is unresponsive/slow elsewhere as well (like in Maps) try restarting the phone (some people claim you have to turn off the phone then pull out the battery to completely de-energized the GPS radio). Hopefully this will bring it back up to it's normally responsive state.

Also, it shouldn't matter, but you said you were using the 1.5 SDK, but you didn't mention if you were compiling against it (as opposed to 1.1). I only bring this up because the GPS works better/faster in Cupcake, so I was wondering what your phone was actually running.

UPDATE:

I wrote up a little test on my own to see if I could duplicate your problem. It consists of just three user-generated files and I tested it with SDK 1.1 and 1.5

In the main Activity (I simply used Main.java):

package org.example.LocationTest;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Main extends Activity implements LocationListener{
    private LocationManager myManager;
    private TextView tv;


    /********************************************************************** 
     * Activity overrides below 
     **********************************************************************/
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     // get a handle to our TextView so we can write to it later
     tv = (TextView) findViewById(R.id.TextView01);

     // set up the LocationManager
     myManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    }

    @Override
    protected void onDestroy() {
     stopListening();
     super.onDestroy();
    }

    @Override
    protected void onPause() {
     stopListening();
     super.onPause();
    }

    @Override
    protected void onResume() {
     startListening();
     super.onResume();
    }



    /**********************************************************************
     * helpers for starting/stopping monitoring of GPS changes below 
     **********************************************************************/
    private void startListening() {
     myManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 
      0, 
      0, 
      this
     );
    }

    private void stopListening() {
     if (myManager != null)
      myManager.removeUpdates(this);
    }




    /**********************************************************************
     * LocationListener overrides below 
     **********************************************************************/
    @Override
    public void onLocationChanged(Location location) {
     // we got new location info. lets display it in the textview
     String s = "";
     s += "Time: "        + location.getTime() + "\n";
     s += "\tLatitude:  " + location.getLatitude()  + "\n";
     s += "\tLongitude: " + location.getLongitude() + "\n";
     s += "\tAccuracy:  " + location.getAccuracy()  + "\n";

     tv.setText(s);
    }    

    @Override
    public void onProviderDisabled(String provider) {}    

    @Override
    public void onProviderEnabled(String provider) {}    

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}

In your "main" layout file (I used main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" 
     android:id="@+id/TextView01"
     android:string="Waiting for location information..." 
    />
</LinearLayout>

Then, in your AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.LocationTest"
    android:versionCode="1"
    android:versionName="1.0">
    <application 
     android:icon="@drawable/icon" 
     android:label="@string/app_name">
     <activity 
      android:name=".Main"
      android:label="@string/app_name">
      <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
     </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

You should be able to compile/run this just fine. If, using this, you still can't get a fix it's not a software problem. You either have a bad GPS or it just can't get a lock on any satellites.

You can download the source here.

fiXedd
Thanks. I have waited for as long as 30 seconds just seeing the GPS icon on the notification bar without actually getting the fix! I am compiling and running against 1.5.
lostInTransit
How is it in maps? Can it find your location in a reasonable time?Did you try power-cycling the phone?
fiXedd
Also, how are you checking to see if the listener got any data? Are you running something in another thread that checks every X amount of time?
fiXedd
The listener methods are not called. Realized later the errors are not related to GPS. Not getting any error other than this. Maps recognizes my position within 5 seconds
lostInTransit
+1  A: 

Here's a nice example with source code http://marakana.com/forums/android/android_examples/42.html

Fedor
A: 

Hello World,

Line 25 gives me problem. ... tv = (TextView) findViewById(R.id.TextView01); ...

The compiler complains about R.id it says "R.id cannot be resolved"

Any hint to solve this ?

Anders
That means your resource files aren't being auto generated. In Eclipse, I recommend trying to rebuild your project. Go to the Project menu, select Clean, and select your current project. Open up the Problems tab/view, to make sure there aren't any errors reported when building the project.
Drew Dara-Abrams
A: 

@ fiXedd

I was having the same problem - location listener never receives an update, etc.... so I downloaded your sample code - created the application, etc. And I get the same exact result. BUT - my GPS seems to work just fine with my other app that I created that uses similar code (and Google Maps finds me just fine) - both locate me fairly quickly...

????

I'm using a Droid running 2.1

FunnyLookinHat
No joke - I rebooted my phone and it worked - so I was going crazy.
FunnyLookinHat
A: 

I'm having a similar issue: I run Google Maps on my Samsung Captivate (2.1 Update Android) and it finds my location instantly and acts like a GPS...moves when I move, etc.

LocationListener takes a while to fire...sometimes a minute or two, sometimes longer.

I don't get it...plain vanilla code similar to above...Using the listener with params of 2000, 10...so every 2 seconds (minimally) it will look for a change of 10M or more. (Correct?)

If I set it to 0,0 I don't see a huge difference, except that it eats battery power faster.

Why does Maps work so well, and the SDK so slow?

I tried the trick with running ATT Navigator, letting it sit on the splash screen, then saying EXIT instead of Accept, and it did get the GPS to start "locking" on my stationary location, but it still takes way too long.

I was hoping to write a little "mark my location" app, but at this point I'm starting to think the SDK is buggy, and Google Maps (Android app) is using different code or something.

Can someone post a quick snippet to find my location and show the point on the map that works as expected?

Mark Turkel