问题背景
有些视频类APP在进行播放时,若是下拉通知栏则会让播放暂停,取消通知栏则会继续播放。
一般情况下,通过生命周期回调方法onPause、onStop之类的可以监听到页面变化,进而来暂停、播放视频即可。
但,对于通知栏下拉这种情况,以上方法无法收到回调监听。
解决方法
/**
* Called when the current {@link Window} of the activity gains or loses
* focus. This is the best indicator of whether this activity is the entity
* with which the user actively interacts. The default implementation
* clears the key tracking state, so should always be called.
*……
*
* <p>As a general rule, however, a foreground activity will have window
* focus... unless it has displayed other dialogs or popups that take
* input focus, in which case the activity itself will not have focus
* when the other windows have it. Likewise, the system may display
* system-level windows (such as the status bar notification panel or
* a system alert) which will temporarily take window input focus without
* pausing the foreground activity.
*
*……
*/
public void onWindowFocusChanged(boolean hasFocus) {
}
可以很明显注释说明中看到:通知栏和系统弹窗弹出时会暂时获取窗口焦点,而并不会pause前台的activity(取消则会让前台activity重新获取焦点)。