我正在尝试将从gps / network接收的位置输出到短信中。我尝试过使用geocode并使用try catch作为List,但是我需要帮助才能将getAddressLine输出到Button setOnClickListener中。这是我的代码。谢谢。
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//conversion
Criteria criteria = new Criteria();
String bp = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bp);
double lat = location.getLatitude();
double lon = location.getLongitude();
final Geocoder gc = new Geocoder(this, Locale.getDefault());
try{
List<Address> address;
address = gc.getFromLocation(lat, lon, 1);
Address ad = address.get(0);
StringBuffer geo = new StringBuffer();
geo = geo.append(ad.getAddressLine(0));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//ends here
LocationListener ll = new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("Location", location.toString());
}
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
}
};
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
按钮的代码
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent loc = new Intent (Intent.ACTION_VIEW);
loc.putExtra("sms_body", geo);
loc.setType("vnd.android-dir/mms-sms");
startActivity(loc);
}
});
我想在loc.putExtra上显示意图的输出。
答案 0 :(得分:0)
您在try / catch块中本地创建geo
变量:您需要将其移动到某种类型的全局变量,因此当您单击时geo
变量实际存在按钮。
如果你这样做(以及你的Geocoder
实例工作正常),那对你有用。