Mobile_Application_Developm.../DailyHelper/app/src/main/java/at/fhj/swd/dailyhelper/service/LocationService.java

110 lines
3.9 KiB
Java

package at.fhj.swd.dailyhelper.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.swd.dailyhelper.R;
import at.fhj.swd.dailyhelper.ui.activity.MainActivity;
import static at.fhj.swd.dailyhelper.DailyHelperApplikation.CHANNEL_ID;
public class LocationService extends Service {
private static final String TAG = "LocationService";
private Notification notification;
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationCallback locationCallback;
@Override
public void onCreate() {
super.onCreate();
setupNotification();
Log.d(TAG, "service created");
fusedLocationProviderClient = 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())
.putFloat("altitude", (float) locationResult.getLocations().get(0).getAltitude())
.putFloat("accuracy", locationResult.getLocations().get(0).getAccuracy())
.putFloat("speed", locationResult.getLocations().get(0).getSpeed()).apply();
}
}
};
}
public void startLocationUpdate() {
LocationRequest request = new LocationRequest();
request.setInterval(5000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.requestLocationUpdates(request, locationCallback, null);
}
}
public void stopLocationUpdate() {
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "service started");
startForeground(4545, notification);
startLocationUpdate();
return START_STICKY;
}
@Override
public void onDestroy() {
stopLocationUpdate();
super.onDestroy();
Log.d(TAG, "service destroyed");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void setupNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentText("Updating location")
.setContentTitle("DailyHelper Location Service")
.setSmallIcon(R.drawable.ic_location_on)
.setContentIntent(pendingIntent).build();
}
}