按两次电源按钮时拨打电话

时间:2015-04-04 13:58:59

标签: android button call

我对android很新。我想知道当按两次电源按钮时是否可以拨打指定的号码。

1 个答案:

答案 0 :(得分: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" />