Assume I have the AOSP source code, How can I pause the APP in the foreground when pulling down the notification panel? I've googled and find an APP can listen the event onWindowFocusChange and take some action proactively, but how can I pause ANY APP when pulling down the notification panel, without modify every APP respectively (which is impractical)?

有没有办法从SystemUI进程调用任何前台App的onPause函数?

推荐答案

You can use below method for detecting the notification panel pull,

在你的 list 文件里

 <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />

在您的活动中,覆盖onWindowFocusChanged()并编写以下代码.

This uses the permission

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    try
    {
        if(!hasFocus)
        {
            Object service  = getSystemService("statusbar");
            Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
            Method collapse = statusbarManager.getMethod("collapse");
            collapse .setAccessible(true);
            collapse .invoke(service);
        }
    }
    catch(Exception ex)
    {
        if(!hasFocus)
        {
            try {
                Object service  = getSystemService("statusbar");
                Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
                Method collapse = statusbarManager.getMethod("collapse");
                collapse .setAccessible(true);
                collapse .invoke(service);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();                
            }
            ex.printStackTrace();
        }
    }
}

Then in your app request for audio focus, refer below link

https://developer.android.com/guide/topics/media-apps/audio-focus#java

This will pause audio access for all other apps.

Kotlin相关问答推荐

Fleet无法从构建文件自动重新加载更改错误'

在Webflux应用程序中通过kotlin协程启动fire and forget job

可选的.在kotlin中不使用泛型参数

如何接受任何派生类KClass

Kotlin:类型不匹配:推断的类型已运行,但应等待

如何在Android应用判断上运行多个查询

通过快捷键手动砍掉功能参数等

Kotlin Path.useLines { } - 如何不获取 IOException("Stream closed")?

如何在 jOOQ 中两次加入同一张表?

PRDownloader 即使在实现库后也无法工作.未知参考:Android Studio 中的 PRDownloader

参考 Kotlin 中的 Java 接口静态字段

IntelliJ 不会根据 ktlint 的期望对 Kotlin 导入进行排序

Kotlin-Java 互操作不能与可变参数一起使用

零安全的好处

以编程方式重新启动 Spring Boot 应用程序/刷新 Spring 上下文

如何在 Spring WebFlux 的响应正文中流式传输二进制数据

如何使用Kotlin Dokka记录主构造函数参数

Kotlin惰性默认属性

Kotlin替换字符串中的多个单词

如何在 Fragment 中使用 Anko DSL?