Sony SmartWatch2 : hooking back key

2019-04-12 11:42发布

问题:

In control extension for Sony SmartWatch2, I can receive back key through onKey, but how can I prevent extension from terminating? I want to hook back key to do some process, but pressing back key terminates extension.

In SampleAdvancedControlExtension, it seems like it is blocking back button by starting new control, but I'm only using single control.

public void onKey(int action, int keyCode, long timeStamp) {
    Log.v(SampleExtensionService.LOG_TAG, "onKey");

    if (action == Control.Intents.KEY_ACTION_RELEASE
            && keyCode == Control.KeyCodes.KEYCODE_BACK) {
        Log.d(SampleExtensionService.LOG_TAG, "onKey() - back button intercepted.");
        onBack();
    } else if (mCurrentControl != null) {
        super.onKey(action, keyCode, timeStamp);
    }
}

/**
 * Closes the currently open control extension. If there is a control on the
 * back stack it is opened, otherwise extension is closed.
 */
public void onBack() {
    Log.v(SampleExtensionService.LOG_TAG, "onBack");
    if (!mControlStack.isEmpty()) {
        Intent backControl = mControlStack.pop();
        ControlExtension newControl = createControl(backControl);
        startControl(newControl);
    } else {
        stopRequest();
    }
}

Ok, I figured out the problem. I had to add following method in RegistrationInformation class.

@Override
public boolean controlInterceptsBackButton() {
    // Extension has it's own navigation, handles back presses.
    return true;
}

回答1:

In "onBack()" method the call to "stopRequest()" is the one that terminates the extension. In your case you should just put your own logic in this method, so that "stopRequest()" won't be called if you don't need it.