gpt4 book ai didi

android - 如何检测通知面板是否展开?

转载 作者:行者123 更新时间:2023-12-02 13:53:15 26 4
gpt4 key购买 nike

我找到了扩展通知面板的方法

Object sbservice = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method showsb;
if (Build.VERSION.SDK_INT >= 17) {
showsb = statusbarManager.getMethod("expandNotificationsPanel");
} else {
showsb = statusbarManager.getMethod("expand");
}
showsb.invoke(sbservice);

查看源代码后,我还发现了collapsePanels方法来折叠通知

但是我找不到任何方法来检测通知面板状态

因为我想检查它是打开还是关闭,然后决定应该打开它还是关闭它

我怎样才能知道这个状态?

最佳答案

虽然很晚了,但可能对某人有帮助

引用this

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

In your manifest file

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

In your activity override the onWindowFocusChanged() and write the below code.

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();
}
} }

关于android - 如何检测通知面板是否展开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33278006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com