Unable to get GPS coordinates on Android
I am trying to obtain the GPS coordinates with the following code:
public class conTest extends Activity implements LocationListener{
TextView headerText;
BufferedReader in = null;
private LocationManager lm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVID开发者_JAVA百科ER,0,0,this);
}
public void onLocationChanged(Location location) {
if (location != null) {
Toast.makeText(getApplicationContext(),"Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude(),Toast.LENGTH_SHORT).show();
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Problem is that when i try to send gps coordinates using DDMS, a toast will appear and say that the location is 0.0,0.0. This is not what i entered. When i try to send a new and different location, nothing happens.
I have allowed mock locations on the emulator and the LOCATION_FINE/INTERNET permissions has been set. I am developing in Eclipse 3.5 using Api 2.1Help! What have I done wrong?
精彩评论