Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能
由于项目没有设计返回键,一旦进入别的应用,就无法回到桌面。只能通过串口输入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(); }
总结
到此这篇关于Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能的文章就介绍到这了,更多相关Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)