From 6d6db2efe00829c7b7413d0104b08d460e401f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Tue, 9 Oct 2018 21:17:51 +0200 Subject: [PATCH] Add working background task in Android 6 (API 23) --- .../BackgroundLocalizationService.java | 73 ++++++++++++++++++- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java index 3efedf5..cc19101 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java @@ -1,12 +1,14 @@ package com.uam.wmi.findmytutor.service; import android.Manifest; +import android.annotation.SuppressLint; import android.app.Notification; import android.app.Service; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationManager; +import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -15,18 +17,28 @@ import android.support.v4.app.NotificationCompat; import android.util.Log; import android.content.Context; +import com.uam.wmi.findmytutor.model.Coordinate; +import com.uam.wmi.findmytutor.network.ApiClient; +import com.uam.wmi.findmytutor.utils.PrefUtils; + import java.util.Timer; import java.util.TimerTask; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.observers.DisposableSingleObserver; +import io.reactivex.schedulers.Schedulers; +import timber.log.Timber; + public class BackgroundLocalizationService extends Service { public static String str_receiver = "background.location.broadcast"; private static final String TAG = "MyLocationService"; private LocationManager mLocationManager = null; - private static final int LOCATION_INTERVAL = 10000; + private static final int LOCATION_INTERVAL = 1000; private static final float LOCATION_DISTANCE = 10f; private Handler mHandler = new Handler(); - private static long notify_interval = 100000; + private static long notify_interval = 10000; Location mLastLocation; Intent intent; LocationManager locationManager; @@ -218,7 +230,8 @@ public class BackgroundLocalizationService extends Service { private void fn_update(Location location){ intent.putExtra("latitude",location.getLatitude()); intent.putExtra("longitude",location.getLongitude()); - sendBroadcast(intent); + new Task(location).execute(); + // sendBroadcast(intent); } private void sendToBroadcast(Location location) { @@ -226,7 +239,8 @@ public class BackgroundLocalizationService extends Service { intent.putExtra("latitude",location.getLatitude()); intent.putExtra("longitude",location.getLongitude()); - sendBroadcast(intent); + new Task(location).execute(); + //sendBroadcast(intent); } private void getLocation() { @@ -258,4 +272,55 @@ public class BackgroundLocalizationService extends Service { } } + private class Task extends AsyncTask { + private Double latitude; + private Double longitude; + + private CompositeDisposable disposable = new CompositeDisposable(); + private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext()) + .create(CoordinateService.class); + + private Task(Location location) { + this.latitude = location.getLatitude(); + this.longitude = location.getLongitude(); + } + + + @Override + protected Object doInBackground(Object[] objects) { + + try { + + Coordinate coordinate = new Coordinate( + this.latitude, + this.longitude, + PrefUtils.getUserStatus(getApplicationContext()), + PrefUtils.getUserId(getApplicationContext()) + ); + + disposable.add( + coordinateService + .postCoordinate(coordinate) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribeWith(new DisposableSingleObserver() { + @SuppressLint("LongLogTag") + @Override + public void onSuccess(Coordinate coord) { + Log.e("CoordinateService onSuccess", String.valueOf(coord)); + } + + @Override + public void onError(Throwable e) { + Log.e("LoginError", "onError: " + e.getMessage()); + } + })); + } catch (IllegalArgumentException e) { + Timber.e(String.valueOf(e)); + } + + return null; + } + } + } \ No newline at end of file