2015-04-04 151 views

回答

0

是的,它是可能的,你可以覆蓋電源按鈕的點擊事件是這樣的:

long last_click = 0; 

    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) { 
     if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) { 
      // Check if power button was pressed twice in last second 
      if ((System.currentTimeMillis() - last_click) <= 1000) { 
       // Make call if pressed twice 
       call(); 
       return true; 
      } 
      last_click = System.currentTimeMillis(); 
     } 
     return super.dispatchKeyEvent(event); 
    } 

    public void call() { 
     Intent callIntent = new Intent(Intent.ACTION_CALL); 
     callIntent.setData(Uri.parse("tel:" + phone)); 
     startActivity(callIntent); 
    } 

添加通話許可清單中:

<uses-permission android:name="android.permission.CALL_PHONE" /> 
+0

感謝u爲代碼。但是在這一行「dispatchKeyEvent(KeyEvent event)」中得到一個錯誤,它說這個方法必須返回一個布爾類型的結果。 – nan0133 2015-04-04 16:08:42

+0

我修改了代碼 – user1888162 2015-04-04 17:46:56

相關問題