永远不会调用android.location.LocationManager的onLocationChanged

时间:2014-08-29 15:02:57

标签: android gps android-location

我有一项服务,可以为应用的其余部分提供位置更新。我使用的是谷歌播放服务的融合位置提供商,但I need to know the actual provider所以我选择了安卓的LocationManager。问题是,onLocationChanged(Location loc)永远不会被调用,onProviderEnabledonProviderDisabled会被调用。

MyService(扩展Service实现LocationListener)onCreate():

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

...

  public void onCreate() {
    super.onCreate();

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = buildCriteria();
    List<String> bestProviders = locationManager.getProviders(criteria, true); // returns 'gps' on test device
    Log.d(TAG, "Found " + bestProviders.size() + " providers");
    try {
      for (String provider : bestProviders) {
        locationManager.requestLocationUpdates(provider, 0, 0, this);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  private Criteria buildCriteria() {
    Criteria res = new Criteria();
    res.setAccuracy(Criteria.ACCURACY_FINE);
    res.setPowerRequirement(Criteria.POWER_HIGH);
    res.setAltitudeRequired(false);
    res.setBearingRequired(false);
    res.setCostAllowed(false);
    res.setSpeedRequired(false);

    return res;
  }

的AndroidManifest.xml

  <uses-permission android:name="android.permission.VIBRATE"/>
  <uses-permission android:name="ee.mtakso.driver.permission.MAPS_RECEIVE"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.WAKE_LOCK"/>
  <uses-permission android:name="android.permission.ACCESS_GPS" />

我正在使用Android 4.4.2,GPS,LTE和Wifi对LG G3进行测试。

0 个答案:

没有答案