带系统警报窗口的后退事件 [英] Back pressed events with system alert window
问题描述
我需要在按下back
和home
按钮事件时关闭系统警报窗口.我尝试使用onKeyEvent
,但徒劳无功.由于我们无法捕获服务中的back
按下事件,因此该如何实现?
I need to dismiss system alert window on back
pressed and home
button event.I have tried with onKeyEvent
but in vain. As we can't capture the back
pressed event in a service, how to achieve this?
推荐答案
由于它是托管覆盖窗口的服务,因此它有些棘手,但有可能.
Since it's a service that hosting an overlay window, It's a bit tricky solution but it is possible.
您应该分别处理这两种情况(压下首页按钮和后退按钮).
You should handle these 2 cases separately (overriding home button press, and back button press).
1.覆盖主页按钮:
创建此HomeWatcher类,其中包含一个BroadcastReceiver,它将在按下主屏幕按钮时发出通知.仅在您的窗口出现时注册此接收器.
Create this HomeWatcher class which contains a BroadcastReceiver that will notify when home button was pressed. Register this receiver only when your window comes up.
在服务的onCreate方法内部使用此方法:
Inside your service onCreate method use this:
HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
@Override
public void onHomePressed() {
yourWindow.hide() //means: windowManager.removeView(view);
}
@Override
public void onHomeLongPressed() {
}
});
mHomeWatcher.startWatch();
2.按下后退按钮:
这个想法是创建一个空布局作为窗口类的数据成员, 并将您的视图附加到该视图(即使它是夸张的XML布局).
The idea is creating an empty layout as a data member of your window class, and attach your view to it (even if its an inflated XML layout).
例如,这将成为您的窗口类:
For example, this is gonna be your window class:
public class MyWindow
{
private WindowManager windowManager;
private WindowManager.LayoutParams params;
private View view;
// Add this empty layout:
private MyLayout myLayout;
public MyWindow()
{
windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.your_original_window_layout, null);
// Add your original view to the new empty layout:
myLayout = new MyLayout(this);
myLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
// And show this layout instead of your original view:
public void show()
{
windowManager.addView(myLayout, params);
}
public void hide()
{
windowManager.removeView(myLayout);
}
}
现在创建MyLayout类以覆盖按下后退按钮:
And now create the MyLayout class to override the back button press:
public class MyLayout extends LinearLayout
{
private MyWindow myWindow;
public MyLayout(MyWindow myWindow)
{
super(myWindow.context);
this.myWindow = myWindow;
}
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0)
{
getKeyDispatcherState().startTracking(event, this);
return true;
}
else if (event.getAction() == KeyEvent.ACTION_UP)
{
getKeyDispatcherState().handleUpEvent(event);
if (event.isTracking() && !event.isCanceled())
{
// dismiss your window:
myWindow.hide();
return true;
}
}
}
return super.dispatchKeyEvent(event);
}
}
我知道我说的有点复杂,因为它是由服务托管的系统警报窗口,但是可以正常工作.我也有同样的问题,已经完全解决了. 祝你好运.
I know it's a bit complicated as I said since it's a system alert window hosted by a service, BUT it's working. I have the same issue as well and it has been solved exactly like that. Good luck.
这篇关于带系统警报窗口的后退事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!