Skip to content

Instantly share code, notes, and snippets.

@mhassanist
Last active June 1, 2020 13:28
Show Gist options
  • Save mhassanist/85082c53f84d7bd3a46cd343a9b0594a to your computer and use it in GitHub Desktop.
Save mhassanist/85082c53f84d7bd3a46cd343a9b0594a to your computer and use it in GitHub Desktop.
package com.reviapps.traveldistance.business;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
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 com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.reviapps.traveldistance.data.AppConstants;
import com.reviapps.traveldistance.ui.MainActivity;
import com.reviapps.traveldistance.R;
import com.reviapps.traveldistance.TDApplication;
import com.reviapps.traveldistance.data.LocationDTO;
import org.greenrobot.eventbus.EventBus;
import java.util.concurrent.Executor;
/**
* Starts location updates on background and publish LocationUpdateEvent upon
* each new location result.
*/
public class LocationUpdateService extends Service {
//region data
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 3000;
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest locationRequest;
private LocationSettingsRequest locationSettingsRequest;
//endregion
//onCreate
@Override
public void onCreate() {
super.onCreate();
initData();
}
//Location Callback
private LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location currentLocation = locationResult.getLastLocation();
Log.d("Locations", currentLocation.getLatitude() + "," + currentLocation.getLongitude());
//Share/Publish Location
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
prepareForegroundNotification();
startLocationUpdates();
return START_STICKY;
}
private void startLocationUpdates() {
mFusedLocationClient.requestLocationUpdates(this.locationRequest,
this.locationCallback, Looper.myLooper());
}
private void prepareForegroundNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
AppConstants.CHANNEL_ID,
"Location Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
AppConstants.SERVICE_LOCATION_REQUEST_CODE,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, AppConstants.CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentTitle(getString(R.string.app_notification_description))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.build();
startForeground(AppConstants.LOCATION_SERVICE_NOTIF_ID, notification);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void initData() {
locationRequest = LocationRequest.create();
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(TDApplication.getAppContext());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment