Background Process to scan the location of the user at regular intervals and update the local database even when the app is not open

前端 未结 6 1321
梦毁少年i
梦毁少年i 2021-01-05 10:32

I am creating an app that checks for user locations every half an hour and updates the location of the user in the local database and then runs CRUD queries based on the use

6条回答
  •  囚心锁ツ
    2021-01-05 11:34

    This cannot perform multiple tasks

    Yes, it can. It has only one thread, and so it can only do one simultaneous task.

    i have to get the location of the user and scan the database , update the database (3 tasks)

    I have no idea why you think that is three tasks. You cannot do them simultaneously.

    Your bigger problem with IntentService is that getting location data is asynchronous, and IntentService is not well-suited for calling APIs that themselves are asynchronous.

    But since i feel it would be a long operation with the local database, i feel i should ignore this one.

    The point behind any service is for "a long operation".

    Basically i looking for something like a CRON JOB that runs on a local database while working on the location data

    Use AlarmManager to trigger a WakefulBroadcastReceiver, which then triggers a Service. The Service, in onStartCommand(), forks a background thread to (asynchronously) retrieve the location and update the database. The Service can then call completeWakefulIntent() on WakefulBroadcastReceiver, plus stopSelf() with the startId received in onStartCommand() for this work, plus allow the thread to terminate. If no other commands were received in the interim, the service will shut down.

提交回复
热议问题