2

I am parsing all text messages from the device and extracting words from them. For doing this I first used Service, but the issue with it was that it made application slower or sometimes I got notification that Application is taking longer to run. Alternative to this I used IntentService. But problem with intent service is that whenever I stopped the application, I couldn't see my service running anymore. Alongside I also have to use Alarm Manager to schedule the things. I am planning to use SyncAdapter for doing both of the things, but I don't think it would be a good option to use it. It would be really helpful if there is a better possible for doing this.

Background task might take upto 5-10 minutes for completion and I am planning to run it in every 12 hours. Though I won't be parsing old messages again. So it won't take longer after first time. The task should not end even when application is closed.

Angad Singh
  • 1,032
  • 1
  • 17
  • 36
  • Did you execute your parsing in `onStartCommand` of `Service`? If yes, than it's the reason of your system gave you ANR (App not responding). Try the AsyncTasks, they are pretty designer for this. –  Ekalips May 22 '17 at 13:48
  • I think the real problem is that he did the work in the service on the main thread – Tim May 22 '17 at 13:49
  • I get it. I am doing it in onStartCommand, Services run on UI Thread, so I have to use a Thread/AsyncTask to do things inside service, right? – Angad Singh May 22 '17 at 14:00
  • @AngadSingh `Service` runs in the background but on the main thread (ui thread) but `IntentService` runs on a separate worker thread. – jayeshsolanki93 May 22 '17 at 14:05

1 Answers1

5

Basically IntentService is apt for background tasks which are not tied to the application lifecycle.

But problem with intent service is that whenever I stopped the application, I couldn't see my service running anymore.

You can send updates to UI from intent service by using:

LocalBroadcastManager: how to use LocalBroadcastManager?

Handler: How to Collect info from IntentService and Update Android UI

Also you might want to see this video: The Zen of IntentService. (Android Performance Patterns)

EDIT:
Forget about using IntentService, it stops as the app stops because it runs on the same process as the app.

Since you want your service to work as a job every 12 hours, you could use a 'Scheduled Service'. You can use JobScheduler or Firebase JobDispatcher API

jayeshsolanki93
  • 2,096
  • 1
  • 20
  • 37
  • The only UI element I want to change is the Notification in Notification bar, the service runs independently and makes only changes in the database. Binding it with application won't make a difference. – Angad Singh May 23 '17 at 07:27
  • Actually it wont even make sense to update the app UI from a background service which is not bound to the app. Have you tried showing notification from an `IntentService`? – jayeshsolanki93 May 23 '17 at 07:35
  • Yes, I am showing the progress notification in progress notification bar from IntentService, closing the app stops the progress updates. It works perfectly till the app is running/ – Angad Singh May 23 '17 at 08:12
  • @AngadSingh You can use `startForeground()` with an `IntentService` which will prevent it from stopping until finished. – jayeshsolanki93 May 23 '17 at 09:12
  • It is working! but, the service is getting restarted on its own after completing... – Angad Singh May 23 '17 at 14:00
  • @AngadSingh I don't know what might be causing it. But a JobScheduler could be a better option to use for your use case. Check the answer, I have edited it – jayeshsolanki93 May 23 '17 at 14:17
  • 1
    Problem was with the alarm manager. It's working perfectly. Thanks! – Angad Singh May 23 '17 at 14:23