我有问题,我有一个按钮,当我按下按钮 -> button.setOnclickListener -> 我会得到当前的 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。如果我按下按钮,它会是真的。当应用程序第一次运行时,我该怎么做才能获取我的地址?