-2

So I want my android app services to run even if I remove it from the list of recent app.some thing like wattsapp and facebook how can i do that?

Sarika Rajput
  • 63
  • 2
  • 11

2 Answers2

2

You've to make your service a STICKY Service.

To do that, within your onStartCommand method, return START_STICKY. This will ensure that even if a Service gets stopped, it'll be restarted by the OS.

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

Then from your activity, you just need to start the service once..

startService(new Intent(this, YourService.class));
Rachit
  • 3,173
  • 3
  • 28
  • 45
0

Well Alarm manager helped but I'm still stuck in Async Task though I'have my alarm set at every 20 second sometimes it take more than 20 sec and some times less so I think it's because of Async task where I'm fetching data from database.

Anyways Thanks.

Sarika Rajput
  • 63
  • 2
  • 11