简体   繁体   中英

Long running background task, Android

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM