开发者

Android GPS location updates doesn't work

开发者 https://www.devze.com 2022-12-28 02:12 出处:网络
I am trying to get the GPS location of my HTC magic using the following code : public class TestGPS extends Activity {

I am trying to get the GPS location of my HTC magic using the following code :

public class TestGPS extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    final TextView tv1 = (TextView)findViewById(R.id.tv1);
    final TextView tv2 = (TextView)findViewById(R.id.tv2);
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()
    {

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

        public void onProviderEnabled(String provider)
        {
        }

        public void onProviderDisabled(String provider)
        {
        }

        public void onLocationChanged(Location loc开发者_如何学编程ation)
        {
            tv1.setText(String.valueOf(location.getLatitude()));
            tv2.setText(String.valueOf(location.getLongitude()));
        }
    });
}

}

I waited for 5 minutes 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 HTC Magic with 1.6 SDK.

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


The code looks fine. Make sure you have the ACCESS_FINE_LOCATION permission. Make sure your GPS is not broken -- in other words, does the built-in Maps application know where you are? Make sure your application works on the emulator with a fix you supply via the emulator.

Here is a sample project that uses GPS and that works fine on hardware.

0

精彩评论

暂无评论...
验证码 换一张
取 消