1. 程式人生 > >Android TV長按遙控按鍵開啟滑鼠模式

Android TV長按遙控按鍵開啟滑鼠模式

功能需求

電視盒子的一般介面只需要遙控即可,但是一些複雜的介面,比如瀏覽器中的網頁,焦點可能沒有適配遙控器,這時候如果盒子沒有連線滑鼠或飛鼠,就沒法操作了。可以啟用系統的滑鼠模式,然後用方向鍵移動游標,也是一個備選操作。

程式碼實現

系統本身整合滑鼠模式,改變系統屬性sys.KeyMouse.mKeyMouseState的值就可以設定滑鼠模式的開關。只需要改動PhoneWindowManager即可:

// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

新增長按事件監聽,監聽長按返回鍵的動作:

else if (keyCode == KeyEvent.KEYCODE_BACK) {
   if (down && repeatCount == 0) {
   } else if((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
       mHandler.postDelayed(MouseRunable, 100);
       return -1;
   }
}

長按後改變sys.KeyMouse.mKeyMouseStat系統狀態的屬性:

Runnable MouseRunable = new Runnable(){
     public
void run() { mstate = SystemProperties.get("sys.KeyMouse.mKeyMouseState"); if(mstate.equals("on")) { SystemProperties.set("sys.KeyMouse.mKeyMouseState", "off"); Toast.makeText(mContext, "Diable mouse mode.", Toast.LENGTH_LONG).show(); } else { SystemProperties.set
("sys.KeyMouse.mKeyMouseState", "on"); Toast.makeText(mContext, "Enable mouse mode,long press again to disable.", Toast.LENGTH_LONG).show(); } } };

這樣就實現了長按遙控上的返回鍵,來開啟和關閉滑鼠模式了。