Android: How to detect if current stack of activities (task) moves to background?

前端 未结 8 1595
一生所求
一生所求 2020-12-09 14:24

The official documentation describes tasks as follows:

*All the activities in a task move together as a unit. The entire task (the entire activity sta

8条回答
  •  死守一世寂寞
    2020-12-09 14:46

    I've been looking for a good answer to this question. It seems Andy Zhang had a good idea, but it may need a bit more work to handle other things, like the Back button.

    Another approach which seems promising is to use the ActivityManager.getRunningAppProcesses() method, which will give you a list of RunningAppProcessInfo which each have an "importance" variable which can be used to detect if your process is in the foreground or other states.

    The problem of course is there is no Detection technique here, just a way to tell what is running and its state, but no way to get informed when the state changes.

    So possibly just run a background thread to poll this info. Seems a bit kludgy but it might work.

    My app uses location services (GPS) so it automatically gets called when the location changes, and I can check the RunningAppProcessInfo list when this happens to determine if my App is still in the foreground. Seems like a lot of work, so maybe Andy's approach can be improved on.

提交回复
热议问题