如何判断在安卓系统中是否下拉了通知栏

3
我有一个程序,如果应用程序未运行,则创建通知。为此,我使用了以下代码:
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);   
}

由于Notify.clear()方法与onResume方法不兼容,因此我结合了自己的尝试和Qberticus的建议。虽然有点笨拙,但它完美地发挥了作用。


你指的是什么「干扰」? - IgorGanapolsky
1个回答

0

我不认为有一种方法,但我可能是错的。然而,我认为检测通知栏是否下拉是实现你想要的功能的错误方式。

你应该通过Activity#onResumeActivity#onPause跟踪你的活动是否处于活动状态。例如,如果调用了onResume,则在某个地方设置一个标志,当调用onPause时重置它。如果设置了标志,则表示你的活动已打开,因此不要发送通知。


有没有办法在具有多个活动的应用程序中实现这一点?我尝试了类似于您的建议,但在活动之间进行转换时出现了相同的问题:通知很快被发送,然后在第二个活动恢复时被销毁。 - mbauer14
你可以使用Handler和sendMessageDelayed,为你的应用程序设置适当的10秒钟或其他时间。然后,如果标志仍然被设置,则发布通知。如果从服务中运行,则可以使用HandlerThread,然后使用getLooper并将其传递给Handler - Rich Schuler
4
在隐藏通知栏后,onResume方法没有被执行。 - Francisco Corrales Morales

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接