Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能
作者:祥子Gyx 时间:2023-02-09 17:50:46
由于项目没有设计返回键,一旦进入别的应用,就无法回到桌面。只能通过串口输入input keyevent 4(返回键)来返回桌面,为了方便调试,在Framework 中监听手势,实现返回功能。
在frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager中有这么一段代码
mSystemGestures = new SystemGesturesPointerEventListener(context,
new SystemGesturesPointerEventListener.Callbacks() {
@Override
public void onSwipeFromTop() {
Log.i("gyx","onSwipeFromTop");
if (mStatusBar != null) {
requestTransientBars(mStatusBar);
}
}
@Override
public void onSwipeFromBottom() {
Log.i("gyx","onSwipeFromBottom");
if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_BOTTOM) {
requestTransientBars(mNavigationBar);
}
}
@Override
public void onSwipeFromRight() {
Log.i("gyx","onSwipeFromRight");
sendKeyCode(4);
Log.i("gyx","sendKeyCode 4");
if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_RIGHT) {
requestTransientBars(mNavigationBar);
}
}
@Override
public void onSwipeFromLeft() {
Log.i("gyx","onSwipeFromLeft");
if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_LEFT) {
requestTransientBars(mNavigationBar);
}
}
@Override
public void onFling(int duration) {
if (mPowerManagerInternal != null) {
mPowerManagerInternal.powerHint(
PowerHint.INTERACTION, duration);
}
}
@Override
public void onDebug() {
// no-op
}
@Override
public void onDown() {
mOrientationListener.onTouchStart();
}
@Override
public void onUpOrCancel() {
mOrientationListener.onTouchEnd();
}
@Override
public void onMouseHoverAtTop() {
mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS);
Message msg = mHandler.obtainMessage(MSG_REQUEST_TRANSIENT_BARS);
msg.arg1 = MSG_REQUEST_TRANSIENT_BARS_ARG_STATUS;
mHandler.sendMessageDelayed(msg, 500);
}
@Override
public void onMouseHoverAtBottom() {
mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS);
Message msg = mHandler.obtainMessage(MSG_REQUEST_TRANSIENT_BARS);
msg.arg1 = MSG_REQUEST_TRANSIENT_BARS_ARG_NAVIGATION;
mHandler.sendMessageDelayed(msg, 500);
}
@Override
public void onMouseLeaveFromEdge() {
mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS);
}
});
代码中监听了手势操作,其中onSwipeFromRight()
就是当从屏幕的右侧向左滑动时会触发,只要在这里加入返回的功能即可;
实现返回按键的功能代码如下
private void sendKeyCode(final int keyCode) {
new Thread(new Runnable() {
@Override
public void run() {
try {
// 创建一个Instrumentation对象
Instrumentation inst = new Instrumentation();
// 调用inst对象的按键模拟方法
inst.sendKeyDownUpSync(keyCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
来源:https://blog.csdn.net/weixin_35649059/article/details/105494785
标签:android,PhoneWindowManager,监听屏幕,返回
0
投稿
猜你喜欢
一篇文章带你了解java接口与继承
2022-07-15 02:44:08
Spring Cloud Gateway 服务网关的部署与使用详细讲解
2022-04-09 01:20:00
Android自定义弹框Dialog效果
2022-09-21 02:40:41
详解Java七大阻塞队列之SynchronousQueue
2023-12-16 04:21:30
详解spring boot 使用application.properties 进行外部配置
2023-02-13 02:09:11
Java 获取两个List的交集和差集,以及应用场景操作
2021-11-21 17:49:43
基于java枚举类综合应用的说明
2023-02-16 09:39:25
java中SynchronizedList和Vector的区别详解
2023-08-23 10:13:12
flutter Container容器实现圆角边框
2021-06-17 23:16:23
Android图片加载利器之Picasso扩展功能
2023-05-14 15:12:33
详解Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限
2023-06-28 04:07:44
Java实现Web应用中的定时任务(实例讲解)
2022-08-12 23:40:02
Android Fragment概述及用法
2022-08-23 15:54:22
MyBatis批量添加、修改和删除
2023-12-07 05:13:31
C#实现智能AI五子棋游戏详解
2021-12-10 06:17:38
java 多态与抽象类详解总结
2022-04-19 10:59:18
浅谈Springboot之于Spring的优势
2022-01-08 02:35:29
Java 接口和抽象类的区别详解
2021-07-26 13:30:21
简单聊一聊Java线程池ThreadPoolExecutor
2021-10-23 15:56:58
java多线程实现交通灯管理系统
2022-01-11 07:36:54