如何判断通知栏是否在android中被拉下 [英] How to tell if notification shade is pulled down in android

查看:8
本文介绍了如何判断通知栏是否在android中被拉下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

使用 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 的建议.这有点笨拙,但效果很好.

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.

推荐答案

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

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.

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

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.

这篇关于如何判断通知栏是否在android中被拉下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆