开发者

How to tell if notification shade is pulled down in android

开发者 https://www.devze.com 2023-01-06 05:46 出处:网络
I have a program that creates a notification if the application is not running. To do this, I have used the following code:

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.inFo开发者_StackOverflow社区cus && !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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消