關於 android 返回鍵 代碼實現

來源:互聯網
上載者:User

標籤:

轉自:http://www.dewen.io/q/11313/android+%E6%A8%A1%E6%8B%9F%E8%BF%94%E5%9B%9E%E9%94%AE%E5%8A%9F%E8%83%BD,

 

我測試發現,有兩種方法可行。
①、java.lang.Runtime

  1. Runtime runtime = Runtime.getRuntime();
  2. runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK);
 

②、android.app.Instrumentation

  1. Instrumentation inst = new Instrumentation();
  2. inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
 

都測試成功了。可以實現 按BACK 的效果。

評論 ( 3) • 連結 • 2013-03-20
  • 0給力,贊一個 – 斑駁-neo 2013-03-20
  • 0非常不錯,謝謝您的回答,這兩個辦法都是可行的,就是Instrumentation 是測試環境類比的,不曉得我生產環境用這個有木有問題,還有就是Instrumentation 這個東西不能在主線程用,不過開個線程就可以了,非常感謝 – tlchencheng 2013-04-01
  • 0@tlchencheng 你確定按這個答案做,在任何程式介面都可以實現返回操作? – wangqi0609342013-08-04
  •  

 

關於 android 返回鍵 代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.