Skip to content

Instantly share code, notes, and snippets.

@bkhezry
Created November 5, 2019 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkhezry/d2629a64e4a0d9318962a53b60d9ff17 to your computer and use it in GitHub Desktop.
Save bkhezry/d2629a64e4a0d9318962a53b60d9ff17 to your computer and use it in GitHub Desktop.
package com.github.bkhezry.buildingcollection.service;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.github.bkhezry.buildingcollection.util.Constants;
import io.nlopez.smartlocation.OnLocationUpdatedListener;
import io.nlopez.smartlocation.SmartLocation;
import io.nlopez.smartlocation.location.config.LocationAccuracy;
import io.nlopez.smartlocation.location.config.LocationParams;
import io.nlopez.smartlocation.location.providers.LocationGooglePlayServicesProvider;
public class LocationService extends IntentService implements OnLocationUpdatedListener {
private static final LocationAccuracy ACCURACY_HIGH = LocationAccuracy.HIGH; //HIGH
private SmartLocation smartLocation;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public LocationService() {
super("location-service");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
}
@Override
public void onLocationUpdated(Location location) {
Intent intent = new Intent(Constants.GPS_LOCATION);
intent.putExtra(Constants.GPS_LOCATION, location);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_01", "My Channel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(getApplicationContext(), "channel_01").setAutoCancel(true);
startForeground(1, builder.build());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
boolean toStart = intent.getBooleanExtra("toStart", true);
if (toStart) {
smartLocation = new SmartLocation.Builder(getApplicationContext()).build();
startLocation();
} else {
SmartLocation.with(getApplicationContext()).location().stop();
stopSelf();
}
} else {
SmartLocation.with(getApplicationContext()).location().stop();
stopSelf();
}
return Service.START_STICKY;
}
private void startLocation() {
float distanceHigh = 1f;
long intervalHigh = 1L;
LocationParams locationParams = new LocationParams.Builder()
.setAccuracy(ACCURACY_HIGH)
.setInterval(intervalHigh * 1000)
.setDistance(distanceHigh).build();
LocationGooglePlayServicesProvider provider = new LocationGooglePlayServicesProvider();
provider.setCheckLocationSettings(true);
smartLocation.location(provider).config(locationParams).start(this);
}
@Override
public void onDestroy() {
super.onDestroy();
SmartLocation.with(getApplicationContext()).location().stop();
}
}
@bkhezry
Copy link
Author

bkhezry commented Nov 5, 2019

Intent intent = new Intent(MainActivity.this, LocationService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      ContextCompat.startForegroundService(MainActivity.this, intent);
    } else {
      startService(intent);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment