這篇文章主要講解了Android PhoneWindowManager監(jiān)聽屏幕右側向左滑動實現(xiàn)返回功能的實現(xiàn),內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
成都創(chuàng)新互聯(lián)公司主營企業(yè)營銷型網(wǎng)站建設,為眾多企業(yè)提供了成都品牌網(wǎng)站建設服務,建網(wǎng)站哪家好?擁有多年的成都營銷網(wǎng)站建設流程,能夠為企業(yè)定制化制作網(wǎng)站服務,在公司網(wǎng)站建設維護方面成績突出。
由于項目沒有設計返回鍵,一旦進入別的應用,就無法回到桌面。只能通過串口輸入input keyevent 4(返回鍵)來返回桌面,為了方便調試,在Framework 中監(jiān)聽手勢,實現(xiàn)返回功能。
在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); } });
代碼中監(jiān)聽了手勢操作,其中onSwipeFromRight()
就是當從屏幕的右側向左滑動時會觸發(fā),只要在這里加入返回的功能即可;
實現(xiàn)返回按鍵的功能代碼如下
private void sendKeyCode(final int keyCode) { new Thread(new Runnable() { @Override public void run() { try { // 創(chuàng)建一個Instrumentation對象 Instrumentation inst = new Instrumentation(); // 調用inst對象的按鍵模擬方法 inst.sendKeyDownUpSync(keyCode); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
看完上述內容,是不是對Android PhoneWindowManager監(jiān)聽屏幕右側向左滑動實現(xiàn)返回功能的實現(xiàn)有進一步的了解,如果還想學習更多內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。