我有问题,我有一个按钮,当我按下按钮 - > button.setOnclickListener - >我会得到当前的GPS位置。但是当我没有按下按钮,并且我的应用程序正在运行时我想要获取位置时,它是错误的。我不明白。 这是按钮代码如何获取Android中的当前GPS位置?
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getAdd();
}
});
public void getAdd()
{
Location currentLocation = mLocationClient.getLastLocation();
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
// Location location = params[0];
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(currentLocation.getLatitude(),
currentLocation.getLongitude(), 1);
} catch (IOException exception1) {
exception1.printStackTrace();
} catch (IllegalArgumentException exception2) {
exception2.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String addressText = context.getString(
R.string.address_output_string,
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
mAddress.setText(addressText);
}
}
确实如此。但是,当我的应用程序运行时,我调用getAdd()函数。 Textview mAddress.setText(addressText)为false。如果我按下按钮,它就会成立。我应该怎么做,以便在第一次运行应用程序时获取我的地址?
在onCreate()方法中运行getAdd()函数? – Razgriz
是的,我想我的文本视图setText我的地址,当应用程序运行 –