2017-08-17 51 views

回答

1

我試圖實現同樣的事情,它的工作完美,但對於某些設備不工作..

private void acceptCall() { 

     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        System.out.println("execute input keycode headset hook"); 
        System.out.println("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK)); 
        Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK)); 

       } catch (IOException e) { 
        // Runtime.exec(String) had an I/O problem, try to fall back 
        System.out.println("send keycode headset hook intents"); 
        String enforcedPerm = "android.permission.CALL_PRIVILEGED"; 
        Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
          Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 

        Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
          Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK)); 

        sendOrderedBroadcast(btnDown, enforcedPerm); 
        sendOrderedBroadcast(btnUp, enforcedPerm); 

        System.out.println("Exception " + e.toString()); 
       } 
      } 

     }).start(); 

    } 
+0

OK很大,讓我嘗試一下...感謝 –