I want to notify user when their location is changed. I monitor user's location via "FusedLocationApi.requestLocationUpdates".
Everything works as long as the app lives.
But when i swipe/kill the app, i no longer get any notifications. What should i do to continue getting notifications even if i kill/swipe the app ?
@Override
public void onHandleIntent(Intent intent){
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
locationRequest = new LocationRequest();
locationRequest.setInterval(2000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onConnected(Bundle bundle) {
try{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
} catch (SecurityException ex){ex.printStackTrace();}
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
handleLocationUpdate();
}
private void handleLocationUpdate(){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(BackGroundService.this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(String.format(Locale.getDefault(),
"Lat:%.2f Long:%.2f", location.getLatitude(), location.getLongitude()));
mBuilder.setContentText("Hi, This is Android Notification Detail!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
//startForeground(1, mBuilder.build());
}