How to stop Android service when app is closed

前端 未结 3 1303
情深已故
情深已故 2021-02-01 03:52

I\'m trying to keep service running continously until user close app.

I\'m using startService() method from onCreate() method of my main activi

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 04:23

    I mean intentional app close. After user swipe out app on "recent apps" screen.

    That is not "closing the app", in terms of Android. That is "removing the task" (or sometimes "stopping the task" -- Google is not very consistent on the terminology).

    Your service should be called with onTaskRemoved() when the task is removed. However, that should be both when the user manually removes the task via the overview screen (a.k.a., recent-tasks list) and when the task naturally goes away on Android 4.4 and below because the task is too old.

提交回复
热议问题