Java GPS Android(仅一个)

Java GPS Android(仅一个),java,android,gps,Java,Android,Gps,你好我需要得到位置纬度和位置经度只有一个不需要侦听器位置,任何例子 每个MainActivity.java中的示例 Location location = Myexample.location; Myexample.class扩展了服务 //这里需要代码 问候 但是,请检查真正返回的内容以及它是否适合您的应用程序上下文。否则,您必须使用侦听器模式来获取最新位置 但如果您想获取最新位置,我强烈建议您不要使用getLastKnownLocation。您可以尝试实现如下内容: class My

你好我需要得到位置纬度和位置经度只有一个不需要侦听器位置,任何例子

每个MainActivity.java中的示例

   Location location = Myexample.location;
Myexample.class扩展了服务

//这里需要代码 问候

但是,请检查真正返回的内容以及它是否适合您的应用程序上下文。否则,您必须使用侦听器模式来获取最新位置

但如果您想获取最新位置,我强烈建议您不要使用
getLastKnownLocation
。您可以尝试实现如下内容:

class MyFragment extends Fragment {

    private void makeUseOfNewLocation(Location location) {
        // some magic code which uses location object
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        LocationManager locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                makeUseOfNewLocation(location);
                locationManager.removeUpdates (this);
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}

        };

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
}

它确保您始终只获得一个location对象。

可能的重复:看这里,您只需要使用LocationManager java.lang.SecurityException:“gps”位置提供程序需要访问\u FINE\u位置权限。LocationManager LocationManager=(LocationManager)getActivity().getSystemService(Context.location\u服务); 字符串提供程序=LocationManager.GPS\U提供程序;Location lastKnownLocation=locationManager.getLastKnownLocation(提供者);Toast.makeText(getActivity().getApplicationContext(),String.valueOf(lastKnownLocation.getLatitude()+lastKnownLocation.getLongitude()),Toast.LENGTH_SHORT.show();java.lang.NullPointerException您读过方法文档吗?据说,如果提供程序当前被禁用,则返回null。我认为在其他情况下,您可能会得到
null
。因此,您必须检查它是否不是
null
class MyFragment extends Fragment {

    private void makeUseOfNewLocation(Location location) {
        // some magic code which uses location object
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        LocationManager locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                makeUseOfNewLocation(location);
                locationManager.removeUpdates (this);
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}

        };

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
}