android - 如何判断通知栏是否在android中被拉下

标签 android notifications visible

我有一个程序可以在应用程序未运行时创建通知。为此,我使用了以下代码:

public void onWindowFocusChanged(boolean focus)
{
    inFocus = focus;

    if (inFocus)
    {//If this activity is in focus, fills data and clears notifications
        fillData();
        Notify.clear();
    }
    else
    {
        if (!RespondScreen.inFocus && !ClearDialog.inFocus)
        {
            Creates a notification
        }
    }
}

这工作正常,除非通知栏被拉下。这会导致 Activity 不处于焦点状态,并且由于其他两个 Activity 均未处于焦点状态,因此会创建一个通知。一旦窗帘被拉回,这个通知就会被破坏,但它会给用户带来烦人的、不必要的干扰。是否有一些设置可以用来测试通知阴影是否处于焦点,或者完全以其他方式?

编辑:根据 Qberticus 的建议,我找到了一个可行的解决方案:

    public void onWindowFocusChanged(boolean focus)
{
    if (focus)
    {//If this activity is in focus, fills data and clears notifications
        inFocus = focus;
        fillData();
        Notify.clear();
    }
}

@Override
public void onPause()
{
    super.onPause();
    inFocus = false;
    Handler handler = new Handler();  
    handler.postDelayed(new Runnable() {  
         public void run() {  
             if (!RespondScreen.inFocus && !ClearDialog.inFocus)
             {
                Intent notifier = new Intent();
                notifier.setAction("itp.uts.program.NOTIFY");
                Bundle bundle = new Bundle();
                bundle.putBoolean("StartNotify", true);
                bundle.putBoolean("StartSound", false);
                notifier.putExtras(bundle);
                getApplicationContext().startService(new Intent(notifier));
             }
         }  
    }, 200);   
}

onResume 方法由于某种原因不能与 Notify.clear() 一起使用,所以我结合了我的尝试和 Qberticus 的建议。它有点笨拙,但效果很好。

最佳答案

我不相信有办法,但我可能是错的。但是,我认为检测通知栏何时关闭是执行您想要的操作的错误方法。

您可能应该通过 Activity#onResumeActivity#onPause 跟踪您的 Activity 是否活跃。比如说,如果 onResume 被调用,在某处设置一个标志,当 onPause 被调用时重置它。如果设置了标志,则您的 Activity 已打开,因此不要发送通知。

关于android - 如何判断通知栏是否在android中被拉下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3161458/

相关文章:

android - 如何使用 Google Analytics (Universal Analytics) 配置 Android Google Tag Manager v4

python - 如何在 Python 中显示系统通知?

jquery - 使用 MySql/Node js 创建通知服务器

javascript - jQuery - 如果其他元素变得可见,则更改元素高度

java - 遇到 jpanels 变得可见的困境

Android在手机上浏览SQLite数据库

java - 将项目添加到 ListView 的中间

android - 如何动态管理相对布局中的控件?

android - 如何增加 NotificationCompat 显示的时间

c# - 有条件地显示隐藏 asp.net Gridview 列