Android之在服务中的Window的关闭方法(利用事件分发)
2017-08-02 14:28
489 查看
在智能管家App项目的开发过程中有一个这样的功能,在服务中写一个窗口,之后这个窗口需要通过Back键关闭,但是由于窗口是在服务Service中的,显然我们无法像在Activity中通过回调onBackPressed()方法来关闭窗口,因此在网上查阅了事件分发的相关资料和刘某人程序员的博客后,总算学习到了如何解决该问题。
首先我们完成这个在服务中弹出Window的服务类
除了注意在使用Window时要添加权限
若使用的Android系统版本在API23之后(也就是Android6.0),弹出Window还需要手动为应用允许一个OVERLAY的权限(这与运行时权限无关),详细可以查看我在StackOverflow上看到的回答:
为了方便测试添加一个Button
同时为其添加点击事件
完成之后,我们首先看一下我们遇到的问题的表现效果:按Back键无法关闭Window
因此我们要想实现按Back键关闭Window,我们需要利用Window的布局来拦截Back键的点击事件,之后通过接口将事件传递到服务中,首先我们找到Window的布局
之后我们需要写一个新的线性布局来代替这个不能拦截Back键点击事件的布局
实现代码如下
这段代码的思路需要理清,首先这是一个继承自LinearLayout的布局类,我们可以把他看成和LinearLayout是一样的,之后我们在这个类中创建了一个监听分发事件并根据这个事件做出相应逻辑的接口——DispatchKeyEventListener,之后重写dispatchKeyEvent(KeyEvent event)方法,这个方法在我们点击一些按键时会进行回调(如Back键),在这个方法中我们利用之前创建的接口来完成对应事件发生时的逻辑,有没有发现这个实现方式和Button的setOnClickListener非常相似?这里的setDispatchKeyEventListener就等同于Button中的setOnClickListener。
完成上述步骤后,我们需要将之前提到的布局文件的根布局从LinearLayout改成现在的SessionLinearLayout
最后再在服务中修改布局的属性为SessionLinearLayout,并为布局设置分发事件的监听器,在匿名内部类中完成点击Back事件后的逻辑即可
现在按Back键就能成功关闭窗口了!
http://blog.csdn.net/qq_26787115/article/details/52260393
首先我们完成这个在服务中弹出Window的服务类
package com.liuguilin.keyevevtsample; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.PixelFormat; import android.os.IBinder; import android.view.View; import android.view.WindowManager; import android.widget.Button; public class WindowService extends Service implements View.OnClickListener { //窗口管理器 private WindowManager wm; //view private View mView; //布局参数 private WindowManager.LayoutParams layoutParams; //取消window private Button btnCloseWindow; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); initWindow(); } /** * 初始化Window */ private void initWindow() { //窗口管理器 wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); //布局参数 layoutParams = new WindowManager.LayoutParams(); layoutPar 4000 ams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.flags = //WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | 不能触摸 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; //格式 layoutParams.format = PixelFormat.TRANSLUCENT; //类型 layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; mView = View.inflate(getApplicationContext(), R.layout.layout_window_item, null); btnCloseWindow = (Button) mView.findViewById(R.id.btnCloseWindow); btnCloseWindow.setOnClickListener(this); } @Override public int onStartCommand(Intent intent, int flags, int startId) { //显示window wm.addView(mView, layoutParams); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); } /** * 点击事件 * * @param view */ @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnCloseWindow: //取消window wm.removeView(mView); break; } } }
除了注意在使用Window时要添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>之外,
若使用的Android系统版本在API23之后(也就是Android6.0),弹出Window还需要手动为应用允许一个OVERLAY的权限(这与运行时权限无关),详细可以查看我在StackOverflow上看到的回答:
为了方便测试添加一个Button
<Button android:id="@+id/openWindow" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="打开Window" android:textAllCaps="false"/>
同时为其添加点击事件
findViewById(R.id.openWindow).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startService(new Intent(MainActivity.this, WindowService.class)); } });
完成之后,我们首先看一下我们遇到的问题的表现效果:按Back键无法关闭Window
因此我们要想实现按Back键关闭Window,我们需要利用Window的布局来拦截Back键的点击事件,之后通过接口将事件传递到服务中,首先我们找到Window的布局
mView = View.inflate(getApplicationContext(), R.layout.layout_window_item, null);
之后我们需要写一个新的线性布局来代替这个不能拦截Back键点击事件的布局
实现代码如下
public class SessionLinearLayout extends LinearLayout { private DispatchKeyEventListener mDispatchKeyEventListener; public SessionLinearLayout(Context context) { super(context); } public SessionLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } public SessionLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (mDispatchKeyEventListener != null) { return mDispatchKeyEventListener.dispatchKeyEvent(event); } return super.dispatchKeyEvent(event); } public DispatchKeyEventListener getDispatchKeyEventListener() { return mDispatchKeyEventListener; } public void setDispatchKeyEventListener(DispatchKeyEventListener mDispatchKeyEventListener) { this.mDispatchKeyEventListener = mDispatchKeyEventListener; } //监听接口 public static interface DispatchKeyEventListener { boolean dispatchKeyEvent(KeyEvent event); } }
这段代码的思路需要理清,首先这是一个继承自LinearLayout的布局类,我们可以把他看成和LinearLayout是一样的,之后我们在这个类中创建了一个监听分发事件并根据这个事件做出相应逻辑的接口——DispatchKeyEventListener,之后重写dispatchKeyEvent(KeyEvent event)方法,这个方法在我们点击一些按键时会进行回调(如Back键),在这个方法中我们利用之前创建的接口来完成对应事件发生时的逻辑,有没有发现这个实现方式和Button的setOnClickListener非常相似?这里的setDispatchKeyEventListener就等同于Button中的setOnClickListener。
完成上述步骤后,我们需要将之前提到的布局文件的根布局从LinearLayout改成现在的SessionLinearLayout
<?xml version="1.0" encoding="utf-8"?> <com.liuguilin.keyevevtsample.SessionLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:alpha="0.3" android:background="@color/colorAccent" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/btnCloseWindow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭窗口"/> </com.liuguilin.keyevevtsample.SessionLinearLayout>
最后再在服务中修改布局的属性为SessionLinearLayout,并为布局设置分发事件的监听器,在匿名内部类中完成点击Back事件后的逻辑即可
public class WindowService extends Service implements View.OnClickListener { //窗口管理器 private WindowManager wm; //view private SessionLinearLayout mView; //布局参数 private WindowManager.LayoutParams layoutParams; //取消window private Button btnCloseWindow; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); initWindow(); } /** * 初始化Window */ private void initWindow() { //窗口管理器 wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); //布局参数 layoutParams = new WindowManager.LayoutParams(); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.flags = //WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | 不能触摸 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH ; //格式 layoutParams.format = PixelFormat.TRANSLUCENT; //类型 layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; mView = (SessionLinearLayout) View.inflate(getApplicationContext(), R.layout.layout_window_item, null); btnCloseWindow = (Button) mView.findViewById(R.id.btnCloseWindow); btnCloseWindow.setOnClickListener(this); //监听返回键 mView.setDispatchKeyEventListener(mDispatchKeyEventListener); } /** * 返回鍵监听 */ private SessionLinearLayout.DispatchKeyEventListener mDispatchKeyEventListener = new SessionLinearLayout.DispatchKeyEventListener() { @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (mView.getParent() != null) { wm.removeView(mView); } return true; } return false; } }; @Override public int onStartCommand(Intent intent, int flags, int startId) { //显示window wm.addView(mView, layoutParams); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); } /** * 点击事件 * * @param view */ @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnCloseWindow: //取消window wm.removeView(mView); break; } } }
现在按Back键就能成功关闭窗口了!
相关文章推荐
- Android -- service 利用广播调用服务的方法
- [导入]在父窗口中得知 window.open()出的子窗口关闭事件的方法(定时器实现)
- 【转】Android笔记:触摸事件的分析与总结----Touch事件分发方法dispatchTouchEvent()源码分析
- 关闭页面window.location事件未执行的原因及解决方法
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android 服务(service)的生命周期以及利用bindservice调用服务里面的方法
- android,利用广播接收者调用服务的方法,代码注册广播接收者
- Android利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android中Touch事件分发机制-关于dispatchTouchEvent等方法的研究
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- [android] 代码注册广播接收者&利用广播调用服务的方法
- 一种在父窗口中得知 window.open()出的子窗口关闭事件的方法
- Android 输入管理服务-输入事件向具体应用的分发
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- android事件分发机制——dispatchTouchEvent方法
- Android ViewGroup事件分发机制(requestDisallowInterceptTouchEvent方法)
- 在父窗口中得知 window.open()出的子窗口关闭事件的方法(定时器实现)
- Android自定义控件系列 六:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android -- service 利用广播调用服务的方法
- Android 拖动控件方法(利用onTouch事件,动态调整控件位置)