美文网首页Android开发学习
Android NoteX Q2: 如何在通知栏下拉时暂停视频?

Android NoteX Q2: 如何在通知栏下拉时暂停视频?

作者: 不会飞的小猪 | 来源:发表于2021-03-29 23:02 被阅读0次

问题背景

有些视频类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重新获取焦点)。

相关文章

网友评论

    本文标题:Android NoteX Q2: 如何在通知栏下拉时暂停视频?

    本文链接:https://www.haomeiwen.com/subject/ykgihltx.html