2012-10-23 77 views
5

我正在自定義導航欄到Android 4.0.3.r1,並希望發送像「主頁」和「返回」的關鍵事件。我的應用程序是不是因此係統:如何在android中發送按鍵事件?

IWindowManager mWindowManager = IWindowManager.Stub.asInterface(
       ServiceManager.getService(Context.WINDOW_SERVICE)); 
mWindowManager.injectKeyEvent(ev, false); 

它不工作,因爲我不能沒有系統的應用得到android.permission.INJECT_EVENTS。我怎樣才能做到這一點?

+2

有人編輯我的問題,從「我怎樣才能送關鍵事件在android系統?」到「我如何處理Android中的關鍵事件?」所以我想知道我該如何發送**關鍵事件。 –

回答

0

還有InputConnectionsendKeyEvent功能。 InputConnection僅API級別3

3
BaseInputConnection mInputConnection = new BaseInputConnection(targetView, true); 
mInputConnection.sendKeyEvent(new KeyEvent(...)); 
+0

嘿在這種情況下targetView將是什麼,以及如何提取它,如果用戶在主屏幕上。由於 – user2430771

+0

是什麼'targerView'?... –

+0

@ th3pat3l我想這是目標視圖...... – Virus721

-1

它爲我的作品:

public static void simulateKey(final int KeyCode) { 

    new Thread() { 
     @Override 
     public void run() { 
      try { 
       Instrumentation inst = new Instrumentation(); 
       inst.sendKeyDownUpSync(KeyCode); 
      } catch (Exception e) { 
       Log.e("Exception when sendKeyDownUpSync", e.toString()); 
      } 
     } 

    }.start(); 
} 
+1

Wnen我使用這些方法它給了我關於INJECT_EVENTS權限錯誤。所以我按照這個帖子:http://stackoverflow.com/questions/5383401/android-inject-events-permission?rq=1。當我在清單文件中包含權限android.permission.INJECT_EVENTS時,它會拋出一個授權給系統應用程序的錯誤。我怎樣才能包含這個權限?謝謝 – user2430771

2

你可以試試這個

try 
{ 
    String keyCommand = "input keyevent " + KeyEvent.KEYCODE_MENU; 
    Runtime runtime = Runtime.getRuntime(); 
    Process proc = runtime.exec(keyCommand); 
} 
catch (IOException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
當然

,你可以選擇命令input text ...輸入文本。

+0

嗨,我試圖用這個來實現我的後退按鈕,但它沒有奏效。我正在爲Android 4.4.3構建。我已經搜查了一個星期,但迄今沒有成功。謝謝 – user2430771

+2

如果你使用這種方法,你需要得到根特權 – Huiyu

+1

這需要問題與根作爲 Runtime.getRuntime()。exec(新字符串[] {「su」,「-c」,「輸入keyevent」+ KeyEvent。關鍵代碼 }); – UdayaLakmal

0

振興老話題 - 你可以用比較新的可訪問性API執行主頁和返回 - 看看「performGlobalAction」在這裏: http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html

(尤其是隨着GLOBAL_ACTION_HOME和GLOBAL_ACTION_BACK行動)

當然你將需要的輔助服務,適當的權限,但這並不需要root

+0

我不想聽事件,我想發送事件(返回鍵),如何使用AccessibilityService在代碼中做到這一點? –

0

你可以試試這個。

long now = SystemClock.uptimeMillis(); 
BaseInputConnection mInputConnection = new BaseInputConnection(findViewById(R.id.MainActivity), true); 
KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HOME, 0); 
mInputConnection.sendKeyEvent(down); 

此代碼可以爲我工作。

注:請記住,以取代「R.id.MainActivity」你的活動名稱。

0

這些都不是有效的。使用下面的代碼轉到主屏幕。

Intent home = new Intent(Intent.ACTION_MAIN); 
home.addCategory(Intent.CATEGORY_HOME); 
//home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(home); 

如果您未從活動/片段調用,則可能需要取消對標誌部分的註釋。 回到以下代碼在某些設備上工作。

dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)); 

讓我知道這是否有幫助!

1

下面是一些精密Roman答案

BaseInputConnection mInputConnection = new BaseInputConnection(findViewById(R.id.main_content), true); 
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU); 
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU); 
mInputConnection.sendKeyEvent(kd); 
mInputConnection.sendKeyEvent(ku);