我试过下面的程序。它在eclipse中工作->如果你通过ddms给出纬度和经度值意味着它在模拟器中显示为当前位置....但它没有检测到android手机中的当前位置。
private class mylocationlistener implements LocationListener {
public void onLocationChanged(Location location) {
Date today = new Date();
Timestamp currentTimeStamp = new Timestamp(today.getTime());
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPS){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
String str = "\n CurrentLocation: "+
"\n Latitude: "+ location.getLatitude() +
"\n Longitude: " + location.getLongitude() +
"\n Accuracy: " + location.getAccuracy() +
"\n CurrentTimeStamp "+ currentTimeStamp;
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
tv.append(str);
}
else
{
String s1="GPS activation in process";
Toast.makeText(MainActivity.this,s1,Toast.LENGTH_SHORT).show();
/*alert.setTitle("gps");
alert.setMessage("GPS activation in progress,\n Please click after few second.");
alert.setPositiveButton("OK", null);
alert.show();*/
}
}
else
{
String s2="Enable Gps";
Toast.makeText(MainActivity.this,s2,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
}
}