Android GPS location updates doesn't work
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.
精彩评论