121 lines
3.4 KiB
Java
121 lines
3.4 KiB
Java
package at.fhj.airkoality.service;
|
|
|
|
import android.Manifest;
|
|
import android.app.Notification;
|
|
import android.app.PendingIntent;
|
|
import android.app.Service;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.IBinder;
|
|
import android.preference.PreferenceManager;
|
|
import android.support.v4.app.ActivityCompat;
|
|
import android.support.v4.app.NotificationCompat;
|
|
import android.util.Log;
|
|
|
|
import com.google.android.gms.location.FusedLocationProviderClient;
|
|
import com.google.android.gms.location.LocationCallback;
|
|
import com.google.android.gms.location.LocationRequest;
|
|
import com.google.android.gms.location.LocationResult;
|
|
import com.google.android.gms.location.LocationServices;
|
|
|
|
import at.fhj.airkoality.AirKoalityApplication;
|
|
import at.fhj.airkoality.R;
|
|
import at.fhj.airkoality.ui.activity.MainActivity;
|
|
|
|
|
|
public class LocationService extends Service {
|
|
|
|
private String TAG = "LocationService";
|
|
|
|
private Notification notification;
|
|
private FusedLocationProviderClient fsdpc;
|
|
private LocationCallback locationCallback;
|
|
|
|
|
|
@Override
|
|
public IBinder onBind(Intent intent) {
|
|
return null;
|
|
}
|
|
|
|
|
|
@Override
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
Log.d(TAG, "Service started");
|
|
startForeground(1337, notification);
|
|
startLocationUpdates();
|
|
|
|
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
setupNotification();
|
|
Log.d(TAG, "Service created");
|
|
|
|
fsdpc = LocationServices.getFusedLocationProviderClient(this);
|
|
locationCallback = new LocationCallback() {
|
|
|
|
@Override
|
|
public void onLocationResult(LocationResult locationResult){
|
|
if(locationResult.getLocations().size()!=0){
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LocationService.this);
|
|
preferences.edit()
|
|
.putFloat("latitude", (float) locationResult.getLocations().get(0).getLatitude())
|
|
.putFloat("longitude", (float) locationResult.getLocations().get(0).getLongitude()).apply();
|
|
|
|
}
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
Log.d(TAG, "Service destroyed");
|
|
stopLocationUpdates();
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
private void startLocationUpdates() {
|
|
LocationRequest request = new LocationRequest();
|
|
request.setInterval(5000);
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
|
fsdpc.requestLocationUpdates(request, locationCallback, null);
|
|
}
|
|
}
|
|
|
|
|
|
private void stopLocationUpdates(){
|
|
fsdpc.removeLocationUpdates(locationCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupNotification() {
|
|
Intent notificationIntent = new Intent(this, MainActivity.class);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
|
|
|
|
notification = new NotificationCompat.Builder(this, AirKoalityApplication.CHANNEL_ID)
|
|
.setContentText("Updating location")
|
|
.setContentTitle("AirKoality-lab Location Service")
|
|
.setSmallIcon(R.drawable.ic_phone_android_black_24dp)
|
|
.setContentIntent(pendingIntent).build();
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|