mcharmas / Android-ReactiveLocation

Small library that wraps Google Play Service API in brilliant RxJava Observables reducing boilerplate to minimum.
2.11k stars 312 forks source link

how can wait until GPS catch location #191

Open Isma3ilMohamed opened 5 years ago

Isma3ilMohamed commented 5 years ago

hey everyone i really like this dependency because it save my time but i want to know how to use it in a service and how can make it wait until gps detect device location if i enable gps for first time thanks

cyrixmorten commented 5 years ago

Think the available examples should cover your needs, you can use them directly in a service. However to ensure continuous detection of GPS when the app is closed, you will need to make it a foreground service.

Creating a foreground service is outside the scope of this library.

lør. 4. aug. 2018 18.02 skrev Esmaeil Mohamed notifications@github.com:

hey everyone i really like this dependency because it save my time but i want to know how to use it in a service and how can make it wait until gps detect device location if i enable gps for first time thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mcharmas/Android-ReactiveLocation/issues/191, or mute the thread https://github.com/notifications/unsubscribe-auth/AGV5OITSTFkL1ctPaRYznjqeO-LIYd3Cks5uNcWcgaJpZM4VvC1o .

Isma3ilMohamed commented 5 years ago

hey @cyrixmorten i try this code as you said but it give me null pointer exception `public class GPSTracker extends Service {

private Context context;
LocationRequest request;
ReactiveLocationProvider locationProvider;

private Location location;

private CompositeDisposable disposable = new CompositeDisposable();

@SuppressLint("MissingPermission")
public GPSTracker(Context context) {
    this.context = context;
    location = ((LocationManager)context.getSystemService(LOCATION_SERVICE))
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    setUpLocation(context);
}

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

@SuppressLint("MissingPermission")
private void setUpLocation(final Context context) {
    request = LocationRequest.create() //standard GMS LocationRequest
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setNumUpdates(5)
            .setInterval(100);
    locationProvider =
            new ReactiveLocationProvider(context, ReactiveLocationProviderConfiguration
                    .builder()
                    .setRetryOnConnectionSuspended(true)
                    .build()
            );

    disposable.add(
            locationProvider.getUpdatedLocation(request)

                    .filter(new Predicate<Location>() {
                        @Override
                        public boolean test(Location location) throws Exception {
                            if (location != null) {
                                Timber.e("Location updated");
                                return true;
                            }
                            return false;
                        }
                    })

                    .subscribe(new Consumer<Location>() {
                        @Override
                        public void accept(Location location) throws Exception {
                         setLocation(location);
                        }
                    })
    );

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    disposable.dispose();
}

}`

i don't know why