android 为什么我无法通过NETWORK_PROVIDER获得位置,如下所示?

cfh9epnr  于 2023-01-15  发布在  Android
关注(0)|答案(1)|浏览(87)

这很简单。
但我在日志上什么也没看到。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_selection);

        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                if (location != null) {
                    double longitude = location.getLongitude();
                    double latitude = location.getLatitude();

                    Log.d("MapSelectionActivity", longitude + " " + latitude);
                } else {
                    Log.d("MapSelectionActivity", "location unavailable");
                }
            }

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

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        };

        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

我确信我的手机连接了WiFi接入点,这使得手机能够上网。

bf1o4zei

bf1o4zei1#

首先更改下面的行:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

收件人:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);

就像这里说的:

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper)

... * minTime位置更新之间的最小时间间隔,以毫秒为单位 *...

    • EDIT**我找到了这个教程here,它的用法更简单:
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, this.locationListener);

private final class MyLocationListener implements LocationListener {
      @Override
      public void onLocationChanged(Location locFromGps) {
          // called when the listener is notified with a location update from the GPS
      }
      @Override
      public void onProviderDisabled(String provider) {
         // called when the GPS provider is turned off (user turning off the GPS on the phone)
      }
      @Override
      public void onProviderEnabled(String provider) {
         // called when the GPS provider is turned on (user turning on the GPS on the phone)
      }
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
         // called when the status of the GPS provider changes
      }
}

相关问题