我有一个程序,如果应用程序未运行,则创建通知。为此,我使用了以下代码:
这个方案很好,但当下拉通知栏时就会出现问题。这会导致活动不在焦点中,因为其他两个活动都不在焦点中,所以会创建一个通知。这个通知会在拉回通知栏时立即消失,但它会给用户带来烦人的不必要干扰。有没有一些设置可以用来测试通知栏是否在焦点中,或者完全另一种方法?
编辑:使用Qberticus的建议,我能够找到一个可行的解决方案:
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的建议。虽然有点笨拙,但它完美地发挥了作用。