android-location-tracker icon indicating copy to clipboard operation
android-location-tracker copied to clipboard

Not working in WorkManager

Open waqarv712 opened this issue 4 years ago • 3 comments

I am using this code:

private void getLocationUpdates() {

            TrackerSettings settings =
                    new TrackerSettings()
                            .setUseGPS(true)
                            .setUseNetwork(true)
                            .setUsePassive(true);
//                            .setTimeBetweenUpdates(5000);
//                            .setTimeBetweenUpdates(30 * 60 * 1000)
//                            .setMetersBetweenUpdates(100);

            LocationTracker tracker = new LocationTracker(getApplicationContext(), settings) {
                @Override
                public void onLocationFound(Location location) {
                    // Do some stuff

                    String latlng = location.getLatitude() + ", " + location.getLongitude();

                    Log.d("WorkManager", "onLocationFound: " + latlng);

                }

                @Override
                public void onTimeout() {
                    Log.d("WorkManager", "onTimeout: ");
                }
            };
            tracker.startListening();
    }

in the doWrok() function of WorkManager. But this code works in the Service.

waqarv712 avatar Apr 14 '20 09:04 waqarv712

The tracked is based on the android.location.LocationManager class. So there is no reference on any service of any kind in the library. I'm not familiar with the WorkManager yet, but if there a chance of the tracked being destroyed after the dowork call ?

quentin7b avatar Jun 22 '20 11:06 quentin7b

in doWork() function you can make BroadcastReceiver.

Intent intent = new Intent(context, LocationBroadcastReceiver.class); context.sendBroadcast(intent);

and call the library functions in BroadcastReceiver

hope it will work .. i have not tried but i have seen in some examples

pioneersingh321 avatar Jul 11 '20 02:07 pioneersingh321

WorkManager is used for battery-friendly background tasks, and it is the normal and only clean way to run background processes.

If you want to "track" your location, it mainly makes sense to do so in the background. So support for this should be very high priority.

ballerburg9005 avatar May 01 '23 22:05 ballerburg9005