views:

53

answers:

1

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.

A: 

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.

Qberticus
Is there any way to implement this for an application with multiple activiies though? I tried something similar to your suggestion, but the same problem occurred when transitioning between activities: a notification was quickly sent, and then destroyed when the second activity resumed.
mbauer14
You could use a Handler and sendMessageDelayed and give it say 10 seconds or something appropriate for your application. Then if the flag is still set then post the notification. If running from a service then you can use a `HandlerThread` and then use `getLooper` and pass it to the `Handler`.
Qberticus