如何判断android中的通知栏是否已下拉

发布于 2024-09-08 02:09:57 字数 1669 浏览 0 评论 0原文

我有一个程序,如果应用程序未运行,它会创建通知。为此,我使用了以下代码:

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
        }
    }
}

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

编辑:使用 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'建议。虽然有点笨拙,但效果很好。

I have a program that creates a notification if the application is not running. To do this, I have used the following code:

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
        }
    }
}

This works fine, except when the notification shade is pulled down. This causes the activity to not be in focus, and because neither of the other two activities are in focus, a notification is created. This notification is destroyed as soon as the shade is pulled back up, but it creates an annoying, unnecessary disturbance to the user. Is there some setting I can use to test if the notification shade is in focus, or another way entirely?

Edit: Using Qberticus' advice, I was able to find a workable solution:

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

The onResume method was for some reason not working with Notify.clear(), so I used a combination of my attempt and Qberticus' suggestion. It's a little clumsy, but it works perfectly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮茶煮酒煮时光 2024-09-15 02:09:57

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

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

I don't believe there is a way, but I could be wrong. However, I think detecting when the notification shade is down is the wrong way to go about what you want.

You should probably track if you're activity is the active one via Activity#onResume and Activity#onPause. Say, if onResume is called set a flag somewhere and when onPause is called reset it. If the flag is set you're activity is open so don't send the notification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文