-1

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());
}
QnARails
  • 377
  • 1
  • 4
  • 14

2 Answers2

2

this will try to save ur service from killing by swiping out. As some custom rom kill ur app even u use it

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}

Removed from task ?? Ok i will start it again

    @Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Intent myIntent = new Intent(this, NotificationService.class);
    startService(myIntent)
}

Destroyed ??

 @Override
public void onDestroy() {
    super.onDestroy();
    //startService(new Intent(getApplicationContext(), NotificationService.class));

    Intent myIntent = new Intent(this, NotificationService.class);

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();

    calendar.setTimeInMillis(System.currentTimeMillis());

    calendar.add(Calendar.HOUR, 6);

    alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

}
  • Thank you for this solution. I got it working by adding the **`overriden onStart`** method. But i realized, **`onStart`** and **`onHandleIntent`** doesn't play together. So i had to extend from **`Service`** base class instead of **`IntentService`** base class. – QnARails Aug 20 '17 at 09:07
1

You need to use android service or intentservice, depending on what you want to do. Then you can run things in the backgroung even if the user has swiched to another app.

I think you need to use service because you want it to keep running in the backgroun. Intentservice runs only till the end of the operetion.

Her some reading matirials:

IntentService , Services , IntentService vs Serviceenter code here

matisa
  • 497
  • 3
  • 25