位置已更改通知android

时间:2012-01-06 15:41:23

标签: android gps location

嘿伙计我刚刚测试了Android中的onLocationChanged方法,我已经实现了一种方法,当我点击一个按钮时,应用程序返回一个带有新lat和lng值的String。但现在我试图在每次位置变化时自动显示结果。这是我到目前为止所做的:

位置经理

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);   

int minTime = 30000;
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
MyLocationListener myLocListener = new MyLocationListener();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);
String bestProvider = locationManager.getBestProvider(criteria, false);

locationManager.requestLocationUpdates(bestProvider, minTime, 1, myLocListener );

onlocationchanged

public void onLocationChanged(Location loc) {

  if(loc != null){

  lat1 = loc.getLatitude();
  lng1 = loc.getLongitude();

  currentlocation = "Location changed to lat: " + lat + "" + "lng: " + lng1 ;

    Context context = getApplicationContext();
    CharSequence text = currentlocation;
    int duration = Toast.LENGTH_LONG;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();


  }

}

我已经将requestlocationupdates方法设置为每60000毫秒请求和更新一次 我的问题是,当位置发生变化时,应用程序永远不会提供lat和lng值。我已经使用模拟器测试了应用程序并尝试telneting lat和lng值。而且我也在车里开车看看价值是否会发生变化。

1 个答案:

答案 0 :(得分:2)

尝试:

currentlocation = "Location changed to lat: " + String.valueOf(lat1) + "" + "lng: " + String.valueOf(lng1) ;

对于测试你可以使用:

requestLocationUpdates(LocationManager.GPS_PROVIDER...