欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能

程序员文章站 2022-06-14 14:27:09
由于项目没有设计返回键,一旦进入别的应用,就无法回到桌面。只能通过串口输入input keyevent 4(返回键)来返回桌面,为了方便调试,在framework 中监听手势,实现返回功能。 在f...

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