【问题标题】:Use GeoCoder in BroadcastReceiver class, Android?在 Android 的 BroadcastReceiver 类中使用 GeoCoder?
【发布时间】:2013-08-06 05:35:55
【问题描述】:

在我的应用程序中,如果用户在广播的帮助下关闭了应用程序,我会尝试在固定时间间隔后找到用户的位置。

但是,在我的广播接收器类中,我得到了用户位置所需的纬度和经度。但是我怎样才能在它的帮助下使用那个 lat 和 long 来找到用户的地址。

地理编码器需要活动上下文,但是我有一个扩展广播接收器的类。

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle b=intent.getExtras();
        Location loc=(Location)b.get(LocationPoller.EXTRA_LOCATION);

        // Getting latitude
        double latitude = loc.getLatitude();

        // Getting longitude
        double longitude = loc.getLongitude();
        System.out.println( loc.getLatitude());
        System.out.println(     loc.getLongitude());

        addGet(latitude,longitude) 
    }

    public void addGet(double latitude,double longitude) {

        Geocoder  geocoder = new Geocoder(this, Locale.getDefault());

        try
        {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            Log.e("Addresses","-->"+addresses);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

【问题讨论】:

    标签: android location broadcastreceiver google-geocoder


    【解决方案1】:

    传递上下文。

        addGet(context, latitude,longitude);
    
        public void addGet(Context context,double latitude,double longitude) {
           Geocoder  geocoder = new Geocoder(context, Locale.getDefault());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多