Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能
由于项目没有设计返回键,一旦进入别的应用,就无法回到桌面。只能通过串口输入inputkeyevent4(返回键)来返回桌面,为了方便调试,在Framework中监听手势,实现返回功能。
在frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager中有这么一段代码
mSystemGestures=newSystemGesturesPointerEventListener(context, newSystemGesturesPointerEventListener.Callbacks(){ @Override publicvoidonSwipeFromTop(){ Log.i("gyx","onSwipeFromTop"); if(mStatusBar!=null){ requestTransientBars(mStatusBar); } } @Override publicvoidonSwipeFromBottom(){ Log.i("gyx","onSwipeFromBottom"); if(mNavigationBar!=null&&mNavigationBarPosition==NAV_BAR_BOTTOM){ requestTransientBars(mNavigationBar); } } @Override publicvoidonSwipeFromRight(){ Log.i("gyx","onSwipeFromRight"); sendKeyCode(4); Log.i("gyx","sendKeyCode4"); if(mNavigationBar!=null&&mNavigationBarPosition==NAV_BAR_RIGHT){ requestTransientBars(mNavigationBar); } } @Override publicvoidonSwipeFromLeft(){ Log.i("gyx","onSwipeFromLeft"); if(mNavigationBar!=null&&mNavigationBarPosition==NAV_BAR_LEFT){ requestTransientBars(mNavigationBar); } } @Override publicvoidonFling(intduration){ if(mPowerManagerInternal!=null){ mPowerManagerInternal.powerHint( PowerHint.INTERACTION,duration); } } @Override publicvoidonDebug(){ //no-op } @Override publicvoidonDown(){ mOrientationListener.onTouchStart(); } @Override publicvoidonUpOrCancel(){ mOrientationListener.onTouchEnd(); } @Override publicvoidonMouseHoverAtTop(){ mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS); Messagemsg=mHandler.obtainMessage(MSG_REQUEST_TRANSIENT_BARS); msg.arg1=MSG_REQUEST_TRANSIENT_BARS_ARG_STATUS; mHandler.sendMessageDelayed(msg,500); } @Override publicvoidonMouseHoverAtBottom(){ mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS); Messagemsg=mHandler.obtainMessage(MSG_REQUEST_TRANSIENT_BARS); msg.arg1=MSG_REQUEST_TRANSIENT_BARS_ARG_NAVIGATION; mHandler.sendMessageDelayed(msg,500); } @Override publicvoidonMouseLeaveFromEdge(){ mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS); } });
代码中监听了手势操作,其中onSwipeFromRight()就是当从屏幕的右侧向左滑动时会触发,只要在这里加入返回的功能即可;
实现返回按键的功能代码如下
privatevoidsendKeyCode(finalintkeyCode){ newThread(newRunnable(){ @Override publicvoidrun(){ try{ //创建一个Instrumentation对象 Instrumentationinst=newInstrumentation(); //调用inst对象的按键模拟方法 inst.sendKeyDownUpSync(keyCode); }catch(Exceptione){ e.printStackTrace(); } } }).start(); }
总结
到此这篇关于AndroidPhoneWindowManager监听屏幕右侧向左滑动实现返回功能的文章就介绍到这了,更多相关AndroidPhoneWindowManager监听屏幕右侧向左滑动实现返回功能内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!