On Android, always called onStopJob?

I am trying to implement my first JobService. I understand that the call is onStartJobalmost guaranteed (yes, there are some ribs if I specify some outrageous requirements without expiration). But what about onStopJob? Is it guaranteed to be called?

To be specific, I have work to do in the background. But I have a way to find out when this is done (e.g. sharedPrefs). I was hoping to rely on when onStopJob was called to send a Notification to the user that the task was completed.

Note. onStopJob is a convenient place to send a notification, as I already check if the task has been completed to find out whether to try again.

ps Something to think of might help (I consider it too): If my onStartJobreturns true, how does the system know when my work stops working if it doesn’t call onStopJob?

+4
source share
2 answers

According to the doc, onStopJob is called when the system decides to stop working before it shuts down. You cannot use this API to meet your needs.

I suggest sending a broadcast message as a completion signal when your work is done.

+3
source

:
(1) onStartJob() false.
(2) , onStartJob() false. (, jobScheduler.cancelAll(), onStartJob().)
(3) jobFinished (params, false); onStartJob() true. (4) onStartJob() true, . (5) jobScheduler.cancelAll() onStartJob() true;

:
onStopJob() , ,   , jobFinished().

:
(1) (2) (3) onStopJob() , , , false onStartJob() jobFinished(), .

(4) onStopJob() , , , , 1 ( Xiaomi 4c).

(5) onStopJob() .

:
(1)how does the system know when my job stops running unless it calls onStopJob? jobFinished() false onStartJob(), , .

(2)Is it guaranteed to be called? . , onStopJob(), , .

+3

All Articles