Can Android Wearable apps detect the power button as a KeyEvent?

爷,独闯天下 提交于 2019-12-09 03:42:59

问题


Can Android Wearable apps detect the power button as a KeyEvent or by other means? I would like to run a listener service to detect the wearable power button.

I tried this but no log corresponding to a KeyEvent occurred.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.e(">>>>>>", "FindPhoneActivity event "+event);
    if (keyCode == KeyEvent.KEYCODE_POWER) {

        // Do something here...
        Log.e(">>>>>>", "FindPhoneActivity onKeyDown");
        //event.startTracking(); // Needed to track long presses
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

I have also experimented with two solutions used on normal android phone apps

  1. Registering a Broadcast Receiever
  2. Creating an onPause event with PowerManagement

Both methods described here. Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?

I tested the above with logs attached to the event but neither produced logs.

来源:https://stackoverflow.com/questions/28568015/can-android-wearable-apps-detect-the-power-button-as-a-keyevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!