Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android應用程序模擬手機按鍵

Android應用程序模擬手機按鍵

編輯:關於Android編程

記得以前在做一個C++項目時,需要在某一步操作之後人為用代碼模擬敲鍵盤上的回車鍵(Enter)效果。     出於好奇,這幾天研究了一下Android中手機(或平板)上各種按鍵的鍵值、模擬方法及最終效果。         1、先來看看Android中對按鍵和值的定義方式:    
  1 public static final int KEYCODE_UNKNOWN         = 0;
  2 /** Key code constant: Soft Left key. */
  3 public static final int KEYCODE_SOFT_LEFT       = 1;
  4 /** Key code constant: Soft Right key. */
  5 public static final int KEYCODE_SOFT_RIGHT      = 2;
  6 /** Key code constant: Home key. */
  7 public static final int KEYCODE_HOME            = 3;
  8 /** Key code constant: Back key. */
  9 public static final int KEYCODE_BACK            = 4;
 10 /** Key code constant: Call key. */
 11 public static final int KEYCODE_CALL            = 5;
 12 /** Key code constant: End Call key. */
 13 public static final int KEYCODE_ENDCALL         = 6;
 14 /** Key code constant: '0' key. */
 15 public static final int KEYCODE_0               = 7;
 16 /** Key code constant: '1' key. */
 17 public static final int KEYCODE_1               = 8;
 18 /** Key code constant: '2' key. */
 19 public static final int KEYCODE_2               = 9;
 20 /** Key code constant: '3' key. */
 21 public static final int KEYCODE_3               = 10;
 22 /** Key code constant: '4' key. */
 23 public static final int KEYCODE_4               = 11;
 24 /** Key code constant: '5' key. */
 25 public static final int KEYCODE_5               = 12;
 26 /** Key code constant: '6' key. */
 27 public static final int KEYCODE_6               = 13;
 28 /** Key code constant: '7' key. */
 29 public static final int KEYCODE_7               = 14;
 30 /** Key code constant: '8' key. */
 31 public static final int KEYCODE_8               = 15;
 32 /** Key code constant: '9' key. */
 33 public static final int KEYCODE_9               = 16;
 34 /** Key code constant: '*' key. */
 35 public static final int KEYCODE_STAR            = 17;
 36 /** Key code constant: '#' key. */
 37 public static final int KEYCODE_POUND           = 18;
 38 /** Key code constant: Directional Pad Up key.
 39  * May also be synthesized from trackball motions. */
 40 public static final int KEYCODE_DPAD_UP         = 19;
 41 /** Key code constant: Directional Pad Down key.
 42  * May also be synthesized from trackball motions. */
 43 public static final int KEYCODE_DPAD_DOWN       = 20;
 44 /** Key code constant: Directional Pad Left key.
 45  * May also be synthesized from trackball motions. */
 46 public static final int KEYCODE_DPAD_LEFT       = 21;
 47 /** Key code constant: Directional Pad Right key.
 48  * May also be synthesized from trackball motions. */
 49 public static final int KEYCODE_DPAD_RIGHT      = 22;
 50 /** Key code constant: Directional Pad Center key.
 51  * May also be synthesized from trackball motions. */
 52 public static final int KEYCODE_DPAD_CENTER     = 23;
 53 /** Key code constant: Volume Up key.
 54  * Adjusts the speaker volume up. */
 55 public static final int KEYCODE_VOLUME_UP       = 24;
 56 /** Key code constant: Volume Down key.
 57  * Adjusts the speaker volume down. */
 58 public static final int KEYCODE_VOLUME_DOWN     = 25;
 59 /** Key code constant: Power key. */
 60 public static final int KEYCODE_POWER           = 26;
 61 /** Key code constant: Camera key.
 62  * Used to launch a camera application or take pictures. */
 63 public static final int KEYCODE_CAMERA          = 27;
 64 /** Key code constant: Clear key. */
 65 public static final int KEYCODE_CLEAR           = 28;
 66 /** Key code constant: 'A' key. */
 67 public static final int KEYCODE_A               = 29;
 68 /** Key code constant: 'B' key. */
 69 public static final int KEYCODE_B               = 30;
 70 /** Key code constant: 'C' key. */
 71 public static final int KEYCODE_C               = 31;
 72 /** Key code constant: 'D' key. */
 73 public static final int KEYCODE_D               = 32;
 74 /** Key code constant: 'E' key. */
 75 public static final int KEYCODE_E               = 33;
 76 /** Key code constant: 'F' key. */
 77 public static final int KEYCODE_F               = 34;
 78 /** Key code constant: 'G' key. */
 79 public static final int KEYCODE_G               = 35;
 80 /** Key code constant: 'H' key. */
 81 public static final int KEYCODE_H               = 36;
 82 /** Key code constant: 'I' key. */
 83 public static final int KEYCODE_I               = 37;
 84 /** Key code constant: 'J' key. */
 85 public static final int KEYCODE_J               = 38;
 86 /** Key code constant: 'K' key. */
 87 public static final int KEYCODE_K               = 39;
 88 /** Key code constant: 'L' key. */
 89 public static final int KEYCODE_L               = 40;
 90 /** Key code constant: 'M' key. */
 91 public static final int KEYCODE_M               = 41;
 92 /** Key code constant: 'N' key. */
 93 public static final int KEYCODE_N               = 42;
 94 /** Key code constant: 'O' key. */
 95 public static final int KEYCODE_O               = 43;
 96 /** Key code constant: 'P' key. */
 97 public static final int KEYCODE_P               = 44;
 98 /** Key code constant: 'Q' key. */
 99 public static final int KEYCODE_Q               = 45;
100 /** Key code constant: 'R' key. */
101 public static final int KEYCODE_R               = 46;
102 /** Key code constant: 'S' key. */
103 public static final int KEYCODE_S               = 47;
104 /** Key code constant: 'T' key. */
105 public static final int KEYCODE_T               = 48;
106 /** Key code constant: 'U' key. */
107 public static final int KEYCODE_U               = 49;
108 /** Key code constant: 'V' key. */
109 public static final int KEYCODE_V               = 50;
110 /** Key code constant: 'W' key. */
111 public static final int KEYCODE_W               = 51;
112 /** Key code constant: 'X' key. */
113 public static final int KEYCODE_X               = 52;
114 /** Key code constant: 'Y' key. */
115 public static final int KEYCODE_Y               = 53;
116 /** Key code constant: 'Z' key. */
117 public static final int KEYCODE_Z               = 54;

 

      其實,在源文件KeyEvent.java中總共定義了將近260個鍵值,這裡只給出了27個(Back、Home、數字、大寫字母等)。         2、而要在程序代碼中模擬按鍵的作用,最好是結合線程一起使用,否則有些按鍵在模擬過程中會出現程序異常終止的情況。     如返回鍵Back,鍵值為4,若是直接模擬,就會運行即終止。代碼很簡單,只需要兩句:   1 Instrumentation inst = new Instrumentation(); 2 inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);   Instrumentation和Activity有點類似,只不過Activity是需要一個界面的,而Instrumentation並不是這樣的。     我們可以將它理解為一種沒有圖形界面的,具有啟動能力的,用於監控其他類(用Target Package聲明)的工具類。         3、接下來,老老實實利用線程(Thread)來實現按鍵的模擬效果,當然還用到了消息機制(Handler、Message):     a、首先在onCreate()方法之前定義Handler對象handler,   1 Handler handler;   b、之後在onCreate方法中定義線程並啟動,    
 1 Thread t = new Thread() {
 2     public void run() {
 3         Looper.prepare();
 4         handler = new Handler() {
 5           public void handleMessage(Message msg) {
 6                Instrumentation inst = new Instrumentation();
 7                //inst.sendCharacterSync(msg.what);
 8                inst.sendKeyDownUpSync(msg.what);
 9            }
10         };
11         Looper.loop();
12     }
13 };
14 t.start();
 

 

    注意上述代碼中的注釋部分,實踐證明兩種方法(sendCharacterSync(int keycode),sendKeyDownUpSync(int keycode))都可以達到預期的效果。     c、線程開啟了,就差發送帶按鍵值的消息了,在執行代碼塊中添加以下代碼即可,   1 Message msg = new Message(); 2 //msg.what = KeyEvent.KEYCODE_BACK; 3 msg.what = Integer.parseInt(key_value.getText().toString()); 4 handler.sendMessage(msg);       這裡鍵值是附帶在消息對象的what成員身上的,實現方法很多種,這裡不一一給出了。     可以看到,代碼中注釋部分是將鍵值固定了:       msg.what = KeyEvent.KEYCODE_BACK;     這樣做不管是調試,還是以後運行應用程序來模擬按鍵,都不太實用,每次都要重新設定並運行程序。     所以,本程序采用後面一種方式:       在主界面上多定義一個編輯框組件key_value,模擬之前輸入想要的鍵值即可(需要將鍵值由輸入的String型轉為int型)。     當然,前提是需要對按鍵與對應的值有個大致的了解。         4、經過測試,發現一些按鍵是不能通過模擬來達到真實效果的。     如Home鍵,官方給出的文檔顯示:Home鍵不再對一般應用允許模擬,即設置了權限。     和手機全屏截取API類似,需要System級或者Root級應用才可以實現想要的效果。     其所給出的替代方案也行不通:Called to process key events. You can override this to intercept all key events before they are dispatched to the window.   Be  sure to call this implementation for key events that should be handled normally.         5、下面給出一個比較容易的替代方案,雖然比較山寨,效果還行:    
 1 @Override
 2 public boolean onKeyDown(int keyCode, KeyEvent event) {
 3     if(keyCode == KeyEvent.KEYCODE_BACK){
 4         Intent intent = new Intent(Intent.ACTION_MAIN);
 5         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 6         intent.addCategory(Intent.CATEGORY_HOME);
 7         this.startActivity(intent);
 8         return true;
 9     }
10     return super.onKeyDown(keyCode, event);
11 }

 

    需要注意的是,ACTION和CATEGORY的設置和AndroidManifest.xml文件中一致,     標志位設置為Intent.FLAG_ACTIVITY_NEW_TASK,如果不是,則不是以一個新任務的角色生成,會出現問題。    
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved