0

我正在尝试将从 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 上显示意图的输出。

4

1 回答 1

0

geo在 try/catch 块中本地创建变量:您需要将其移动到某种全局变量,因此geo当您单击按钮时变量实际上存在。

如果您这样做(并且您的Geocoder实例工作正常),那应该对您有用。

于 2013-05-07T00:00:26.773 回答