I'm doing a download process using asyncTask over a webview so that downloading process will be happening until the app gets closed. Again when user opens the app, the downloading process continues. I tried running it in a backgroundService and i started the service once the app is stopped. The problem is service is getting started but when i close the app, the service stops. I also tried displaying notification while service is running but still when i close the app, service will be killed. I referred this post. Kindly suggest me something where it solves me the problem and that should work starting from Android lollipop till the latest.
Asked
Active
Viewed 206 times
1 Answers
0
I would suggest that a foreground service is the answer like in the link just additional with a notification channel
First check how to start the service with:
Intent intent = new Intent(this@MainActivity, YourService::class.java);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startForegroundService(intent);
else
startService(intent);
Then from Service create a Notification channel before you setup the notification:
private NotificationManager createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = NotificationChannel(
CHANNEL_ID, "NAME", NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager::class.java);
manager.createNotificationChannel(serviceChannel);
return manager;
}
return null;
}
Finally after that call:
Notification notification = mBuilder.build();
startForeground(1, notification);
Now should the notification be alive as long as your app is
If this doesn't work write to your registered service in manifest this
<service
android:name=".YourService"
android:stopWithTask="true" />