我正在尝试将从 gps/network 接收到的位置输出到短信中。我曾尝试使用地理编码并使用 try catch 作为列表,但我需要帮助才能将 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 上显示意图的输出。