【发布时间】:2012-01-06 15:41:22
【问题描述】:
我只是在 Android 中测试 onLocationChanged 方法,我已经实现了一种方法,当我单击按钮时,应用程序会返回一个带有新 lat 和 lng 值的字符串。但现在我试图在每次位置更改时自动显示结果。这就是我目前要做的:
location manager
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 );
位置改变了
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 值。我已经使用模拟器测试了该应用程序并尝试远程登录 lat 和 lng 值。而且我还开着我的车到处转转,看看这些值是否发生了变化。
【问题讨论】: