0

我正在尝试使用mcharmas的 Android-ReactiveLocation

什么都没有发生,即它永远不会到达call方法。设备的位置信息已打开。使用 GPS 应用程序,我可以看到 GPS 正在工作。在Settings > Location > Recent location request中,我的应用在列表中排在第一位。没有错误消息。
我肯定错过了什么。它是什么?

我在应用程序 build.gradle 中包含 Reactive Location,如下所示:

compile 'pl.charmas.android:android-reactive-location:0.10@aar'

我在 Google Play 上创建了一个 API 密钥并将其放入清单文件中:

<meta-data
    android:name="com.google.android.geo.API_KEY"
     android:value=aaaaaaaaaaaa-bbbbbbbbbbbbbb_ccccccccccc"/>

在我项目的 gradle.properties 文件中,我添加了:

REACTIVE_LOCATION_GMS_API_KEY="aaaaaaaaaaaa-bbbbbbbbbbbbbb_ccccccccccc"

MainActivity.OnCreate():

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
        PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
            LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION);
}

locationProvider = new ReactiveLocationProvider(getApplicationContext());
lastKnownLocationObservable = locationProvider.getLastKnownLocation();

final LocationRequest locationRequest = LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_LOW_POWER)
        .setInterval(1000); // milliseconds

Subscription locationUpdateSubscription = locationProvider
        .getUpdatedLocation(locationRequest)
        .subscribe(new Action1<Location>(){
            @Override
            public void call(Location location){
                Log.i(TAG, "call: (" + location.getLatitude() + ", " + location.getLongitude() + ")");
                PreferencesUtilities.setCurrentLocation(mActivity.getApplicationContext(), location);
            }
        }
);
4

1 回答 1

1
Observable<Location> locationObservable = reactiveLocationProvider.getUpdatedLocation(
        LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_LOW_POWER)
        .setInterval(1000);

locationObservable.map(location -> {
     //do whatever you want with your location
}).subscribe();

这将起作用,当然可以根据您的需要进行调整

于 2016-11-02T04:54:24.050 回答