Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在单独的类中从LocationListener获取当前城市的结果_Java_Android_Locationmanager - Fatal编程技术网

Java 在单独的类中从LocationListener获取当前城市的结果

Java 在单独的类中从LocationListener获取当前城市的结果,java,android,locationmanager,Java,Android,Locationmanager,嗨,我已经实现了一个位置侦听器来检索当前的城市名称。我遇到的唯一问题是,我不太确定将结果返回给调用类MainActivity.java的最佳方法是什么 这是我代码的一些摘录: 主要活动 这是我在分离的java文件上的LocationListener类 public class MyLocationListener implements LocationListener { private Context mContext; private String mLocality;

嗨,我已经实现了一个位置侦听器来检索当前的城市名称。我遇到的唯一问题是,我不太确定将结果返回给调用类MainActivity.java的最佳方法是什么

这是我代码的一些摘录:

主要活动

这是我在分离的java文件上的LocationListener类

public class MyLocationListener implements LocationListener {

    private Context mContext;
    private String mLocality;
    private String mCountry;

    public MyLocationListener(Context context){
        mContext = context;
    }

    @Override
    public void onLocationChanged(Location location) {
        //You had this as int. It is advised to have Lat/Loing as double.
        double lat = location.getLatitude();
        double lng = location.getLongitude();

        String countryName = null;
        String subAdmin  = null;
        String adminArea  = null;
        String locality  = null;
        String sublocality  = null;

        String longitude = "Longitude: " + location.getLongitude();
        String latitude = "Latitude: " + location.getLatitude();
        Log.i("DEBUG", longitude);
        Log.i("DEBUG", latitude);

        Geocoder geoCoder = new Geocoder(mContext, Locale.getDefault());
        StringBuilder builder = new StringBuilder();
        try {
            List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
            if(address.size() > 0){
                adminArea = address.get(0).getAdminArea().toString();
                countryName = address.get(0).getCountryName().toString();
                subAdmin = address.get(0).getSubAdminArea();
                locality = address.get(0).getLocality();
                sublocality = address.get(0).getSubLocality();
            }
            Log.i("DEBUG", adminArea);
            Log.i("DEBUG", countryName);
            mLocality = adminArea;
            mCountry = countryName;
        } catch (IOException e) {}
          catch (NullPointerException e) {}
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Log.i("DEBUG", "Disabled provider " + provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.i("DEBUG", "Enabled new provider " + provider);
    }


    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}
我试图实现的是在应用程序启动时以及位置更改时显示城市名称

我应该叫onLocationChange还是别的?
谁能给我指一下正确的方向吗?Thx的帮助。

提取onLocationChanged中的所有内容,将其转换为方法或类,并将该方法放入其中。在启动时调用该方法,然后从onLocationChanged

可以让活动实现LocationListener接口。
public class MyLocationListener implements LocationListener {

    private Context mContext;
    private String mLocality;
    private String mCountry;

    public MyLocationListener(Context context){
        mContext = context;
    }

    @Override
    public void onLocationChanged(Location location) {
        //You had this as int. It is advised to have Lat/Loing as double.
        double lat = location.getLatitude();
        double lng = location.getLongitude();

        String countryName = null;
        String subAdmin  = null;
        String adminArea  = null;
        String locality  = null;
        String sublocality  = null;

        String longitude = "Longitude: " + location.getLongitude();
        String latitude = "Latitude: " + location.getLatitude();
        Log.i("DEBUG", longitude);
        Log.i("DEBUG", latitude);

        Geocoder geoCoder = new Geocoder(mContext, Locale.getDefault());
        StringBuilder builder = new StringBuilder();
        try {
            List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
            if(address.size() > 0){
                adminArea = address.get(0).getAdminArea().toString();
                countryName = address.get(0).getCountryName().toString();
                subAdmin = address.get(0).getSubAdminArea();
                locality = address.get(0).getLocality();
                sublocality = address.get(0).getSubLocality();
            }
            Log.i("DEBUG", adminArea);
            Log.i("DEBUG", countryName);
            mLocality = adminArea;
            mCountry = countryName;
        } catch (IOException e) {}
          catch (NullPointerException e) {}
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Log.i("DEBUG", "Disabled provider " + provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.i("DEBUG", "Enabled new provider " + provider);
    }


    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}