Running a repeating task in background on a real time application

后端 未结 2 1000
青春惊慌失措
青春惊慌失措 2021-01-16 17:38

I\'m writing an application which is continuously listening and checking the sensors (almost all available) and saving that data into the database in the device.

I

2条回答
  •  孤城傲影
    2021-01-16 18:25

    You can use AlarmManager to setup the repeating tasks (this is the Android prefered way of setting future/repeating tasks). To make the calculations use a Service (if you think calculations are going to be expensive, then think about moving them to a separate worker thread or use IntentService).

    Regarding the wake lock (from the AlarmManager reference):

    The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

提交回复
热议问题