Add working background task in Android 6 (API 23)

This commit is contained in:
Mieszko Wrzeszczyński 2018-10-09 21:17:51 +02:00
parent 1773b09aa3
commit 6d6db2efe0
1 changed files with 69 additions and 4 deletions

View File

@ -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<Coordinate>() {
@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;
}
}
}