【问题标题】:to make call when power button is pressed twice按两次电源按钮拨打电话
【发布时间】:2015-04-04 13:58:59
【问题描述】:

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

【问题讨论】:

    标签: android button call


    【解决方案1】:

    是的,有可能,您可以像这样覆盖电源按钮单击事件:

    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" />
    

    【讨论】:

    • 感谢您的代码。但是在“dispatchKeyEvent(KeyEvent event)”这一行中出现错误,它表示此方法必须返回布尔类型的结果。
    猜你喜欢
    • 2011-07-21
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多