出于功能需求,需要在所有的view之上显示浮窗,于是需要在WindowManager的View上处理返回键的响应,

mFloatingWindowView =  layoutInflater.inflate(R.layout.floating_window, null, false);

mFloatingWindowLayoutParams = new WindowManager.LayoutParams();
  // 设置window type
mUserConversationWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; mUserConversationWindowParams.format = PixelFormat.TRANSLUCENT;// 设置图片格式,效果为背景透明 // 设置Window flag
mUserConversationWindowParams.flags =
//可使用FLAG_DISMISS_KEYGUARD选项直接解除非加锁的锁屏状态。此选项只用于最顶层的全屏幕窗口。
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL //必须 设置窗口不拦截窗口范围之外事件 |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH // 必须 设置在有FLAG_NOT_TOUCH_MODAL属性时,窗口之外事件发生时自己也获取事件 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
mWindowManager.addView(mFloatingWindowView, mFloatingWindowLayoutParams);

这里千万要注意不能用WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,我就是死在这上面的,如果设置成FLAG_NOT_FOCUSABLE,死都收不到返回键的事件的!

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.LinearLayout; /**
* Created by KB-Shirlman on 4/26/2016.
*/
public class FloatingWindowView extends LinearLayout {
public FloatingWindowView(Context context) {
super(context);
} public FloatingWindowView(Context context, AttributeSet attrs) {
super(context, attrs);
} public FloatingWindowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
|| event.getKeyCode() == KeyEvent.KEYCODE_SETTINGS) {
if(event.getAction()==KeyEvent.ACTION_DOWN){   //按键  按下和移开会有两个不同的事件所以需要区分
closecao(); //点击返回 要执行的方法
}
} return super.dispatchKeyEvent(event); }  }

floating_window.xml

<?xml version="1.0" encoding="utf-8"?>
<YOUR.PACKAGE.NAME.FloatingWindowView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">

</YOUR.PACKAGE.NAME.FloatingWindowView>

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<YOUR.PACKAGE.NAME.FloatingWindowView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1">
    
</YOUR.PACKAGE.NAME.FloatingWindowView>

下面附赠哪都能搜索的到的WidnowManager Home按键监听。

private IntentFilter mHomeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

private BroadcastReceiver mHomeListenerReceiver = new BroadcastReceiver() {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);

if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
&& reason != null && reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
closeFloatingWindow();
}
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private IntentFilter mHomeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
 
private BroadcastReceiver mHomeListenerReceiver = new BroadcastReceiver() {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
 
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
 
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
&& reason != null && reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
closeFloatingWindow();
}
}
};

打开浮窗时调用:

this.registerReceiver(mHomeListenerReceiver, mHomeFilter);
1
this.registerReceiver(mHomeListenerReceiver, mHomeFilter);

关闭浮窗时调用:

this.unregisterReceiver(mHomeListenerReceiver);
1
this.unregisterReceiver(mHomeListenerReceiver);

浮窗WindowManager view返回和Home按键事件监听的更多相关文章

  1. 关于实现自定义Dialog和实现Dialog里view的事件监听的两种方法

    一.自定义dialog. 二.实现dialog里view的事件监听 1.自定义dialog比较简单.在实例化new的时候,加入样式,布局就行了.或者重写dialog. 2.实现dialog里view的 ...

  2. 事件监听:诀别Android繁琐的事件注册机制——view.setOnXXXXListener

    本版本为1.0,支持较少,使用不够方便.相关封装逻辑结构已升级至2.0,详情可参见:更完善的安卓事件监听实现 先简单扯两句这几天学习下来对java事件监听机制的一点感触.客观地讲,java的事件监听机 ...

  3. Qt之键盘事件监听-实时响应大小写Capslock按键

    目录 一.开篇 二.效果展示 三.实现思路 1.重写QLlinEdit 2.全局应用程序事件 3.windows钩子 四.相关文章 原文链接:Qt之键盘事件监听-实时响应大小写Capslock按键 一 ...

  4. IOS微信浏览器返回事件监听问题

    业务需求:从主页进入A订单页面,然后经过各种刷新或点标签加载后点左上角的返回直接返回到主页 采取方法:采用onpopstate事件监听url改变,从而跳转到主页 遇到的问题:安卓上测试没问题:苹果手机 ...

  5. Android View中的控件和监听方法...

    PS:居然三天没写博客了...今天补上...东西虽多,但是都是一些基础...代码多了一些,有人可能会这样问,粘这么多代码有毛用..其实对于一个Android的初学者来说,一个完整的代码是最容易帮助理解 ...

  6. Android 开发中的View事件监听机制

    在开发过程中,我们常常根据实际的需要绘制自己的应用组件,那么定制自己的监听事件,及相应的处理方法是必要的.我们都知道Android中,事件的监听是基于回调机制的,比如常用的OnClick事件,你了解它 ...

  7. HTML5 浏览器返回按钮/手机返回按钮事件监听

    1.HTML5  History对象 支持使用pushState()方法修改地址栏地址,而不刷新页面. popstate事件 当history实体被改变时,popstate事件将会发生.调用pushS ...

  8. js中,实现对键盘按键的监听:

    <script> function keyUp(e) { var currKey=0,e=e||event; currKey=e.keyCode||e.which||e.charCode; ...

  9. JS使用 popstate 事件监听物理返回键

    pushHistory();        window.addEventListener("popstate", function (e) {            if (or ...

随机推荐

  1. 【报错】invalid or unexpected token

    结果发现是把英文的,写成了中文字符,系统没法识别.

  2. Nginx配置语法和日志

    nginx配置 配置文件 重启服务 http请求 nginx日志 一共有两个日志文件 在配置文件中添加这个,就可以在日志文件中看到请求的userAgent 配置语法的检查 nginx重新加载配置 发送 ...

  3. Linux IP怎么设置

    最常用的给网卡配置ip的命令为#ifconfig eth0 192.168.0.1 但是,这样重启后又打回原形.要想永久保存,需要 vim /etc/sysconfig/network-scripts ...

  4. seleniumIDE使用

    1.selenium IDE使用:适用于火狐浏览器 2.界面按钮包括录制(右上角的红点),运行脚本(中上页的绿色三角,包括依次运行和单个运行的2个运行按钮) 3.导出文件为.java,在文件选项中

  5. css中可继承和不可继承属性

    一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定添加到文本的装饰 text-shad ...

  6. TOJ2712: Atlantis

    小数据求面积并 There are several ancient Greek texts that contain descriptions of the fabled island Atlanti ...

  7. 在Asp.net MVC中应该怎样使用Spring.Net

    简单工厂 专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类或接口.简单工厂模式又称为静态工厂方法(Static Factory Method)模式,属于类的创建型模式,通常根据一 ...

  8. shiro实现app和web统一登录

    (转自:http://www.cnblogs.com/sunshine-2015/p/5515429.html)   先说下背景,项目包含一个管理系统(web)和门户网站(web),还有一个手机APP ...

  9. 将Linux下python默认版本切换成替代版本

    本文链接自http://www.myhack58.com/Article/48/66/2016/71806.htm 当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Pyt ...

  10. C语言扩展题

    1.使用cmake来创建c语言工程 2.使用gcc来编译源代码 3.下载redis,并且编译运行redis(注:redis目前是c语言编写的,而且是主要是linux平台,在windows平台编译比较麻 ...