5

我有一个关于 Android 中 LocationListener 的一般性问题。也许这通常与 Android 或 Java 事件有关,但不确定。

似乎有一百万种方法可以设置 LocationListener,而且它们看起来都很丑陋(主要是因为缺乏可重用性)。这是一个来自 android 的例子

// Acquire a reference to the system Location Manager
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.
      makeUseOfNewLocation(location);
    }

    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);

这行得通,但是,我真的很惊讶这是编写 OOP 代码的标准方式......

我真正希望看到的是一种实现LocationListener的简单可重用方式。任何人都知道如何做到这一点的简单教程?我的目标是能够轻松地实现任何需要使用 gps 信息更新的活动......也许是服务?谢谢!

4

0 回答 0