0

I have tasks in my application that run in the background. These tasks can be performed for a long time an hour or more.

There are two questions:

1) I need to show the user the status of the task. How to do this? Every time create a notification? What is the best practice how to solve this issue?

2) Is it possible to make that intent service performs the task to the end and not be disconnected?

Delphian
  • 1,650
  • 3
  • 15
  • 31

1 Answers1

2

If you want your service to keep running after completing of the task IntentService is not the best choice. You can use Foreground service to achieve your both requirements - Show notification to the user and long running task

Edit: Because foreground service is just normal service it is running on the main thread, so you`ll need to use Async task for the actual background work (or any other way of doing async work). On the plus side- using foreground service will make the system less likelly to stop your long running work

X3Btel
  • 1,408
  • 1
  • 13
  • 21
  • I did not think about it, also an idea. The service will only be displayed in notification bar? What are the possible problems with this solution? – Delphian Apr 04 '17 at 08:59
  • The service wont be 'displayed' in the nottification bar. It will just run as normal service but will be threaded as it is in the foreground a.k.a system will try to not kill it. @Delphian ive made edit with pro and con – X3Btel Apr 04 '17 at 09:23
  • I added startForeground() instead of NotificationManager.notify() and my intentService doesn't killed and work if the app is closed. But when I click my notification and the app start again my intentService stops. How to change this? – Delphian Apr 10 '17 at 09:33
  • What is your nottification intent? Maybe you are restarting/cancelling the service there? – X3Btel Apr 10 '17 at 11:00
  • My Pending intent is : PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); but I found the problem. Thank you! I want to ask one more qouestion: I am using startForeground(NOTIFICATION_ID, mNotification.build()); I can't delete notification from notification bar when the service is stoped? – Delphian Apr 10 '17 at 14:12
  • And what FLAG in pending intent more prefereble? Thank you for your time! – Delphian Apr 10 '17 at 14:13
  • @Delphian stopForeground(true) docs: https://developer.android.com/reference/android/app/Service.html#stopForeground(boolean) – X3Btel Apr 10 '17 at 14:22
  • Yes, I did this. Apparently, my mistake was that I did startForeground() several times and only one stopForeground(). Thank you! – Delphian Apr 10 '17 at 14:35