DEV Community

HarmonyOS
HarmonyOS

Posted on

[Smart Watch] [API 6] How does a smartwatch monitor incoming call status, ongoing calls, and call endings?

Read the original article:[Smart Watch] [API 6] How does a smartwatch monitor incoming call status, ongoing calls, and call endings?

Issue

How does a smartwatch monitor incoming call status, ongoing calls, and call endings?

Solution

public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);
    LogUtil.error(TAG, "---onStart:---" );
    context = this;
    initCallManager();
    callListener();
}

private void initCallManager() {
    mDistributedCallManager = DistributedCallManager.getInstance(context);
    mWearCallStateObserver = new WearCallStateObserver();
}

private void callListener() {
    LogUtil.error(TAG, "callListener:" + mDistributedCallManager);
    if (this.mDistributedCallManager != null) {
        LogUtil.error(this.TAG, "CallStateObserver");
        this.mDistributedCallManager.addObserver(mWearCallStateObserver, CallStateObserver.OBSERVE_MASK_CALL_STATE);
    }
}

private class WearCallStateObserver extends CallStateObserver {
    public WearCallStateObserver() throws IllegalArgumentException {
        super();
    }

    public void onCallStateUpdated(int state, String number) {
        super.onCallStateUpdated(state, number);
        LogUtil.error(TAG, "onCallStateUpdated:" + state);
        if (state == CALL_STATE_OFFHOOK) {
            CallStateObserver.CALL_STATE_RINGING(Number 1); // Call status
            CallStateObserver.CALL_STATE_OFFHOOK(Number 2); // In a call
            CallStateObserver.CALL_STATE_IDLE (Number 0); // Hang up 
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Written by Emirhan Serin

Top comments (0)