Search code examples
c#windows-phoneuwp

How to detect call hang up in windows phone 10


I want to programmatically detect when call hang up in UWP app for windows phone 10.

I see Caller ID sample and see Communication blocking and filtering sample too.

But I don't find simple solution for detect when call hang up. I found CallHistoryChanged trigger and I think my complex solution is read call history when call history changed trigger and get last incoming call.

Is there any simple solution for detect call hang up?

Is my solution is correct? Is there a better solution than my solution?


Solution

  • Is there any simple solution for detect call hang up? Is my solution is correct? Is there a better solution than my solution?

    As far as I know, there is no other simple solution for detect calls' hang up.

    There is an event PhoneCallManager.CallStateChanged, which can also detect hang up like below:

    private bool callCame = false;
    
    PhoneCallManager.CallStateChanged += PhoneCallManager_CallStateChanged;
    
    private async void PhoneCallManager_CallStateChanged(object sender, object e)
    {
        if (callCame&&(!PhoneCallManager.IsCallActive))
        {
            //do something
        }
        if (PhoneCallManager.IsCallIncoming)
        {
            callCame = true;
        }
    }
    

    But I don't think it's better than CallHistoryChanged trigger. And it won't get you the last Hang up phone's number either.

    Is my solution is correct?

    So yes, your solution is correct.

    Update: There is no trick here. Register the BackgroundTask:

    private void btnClick_Click(object sender, RoutedEventArgs e)
    {
        var taskRegistered = false;
        var exampleTaskName = "MyBackgroundTask";
    
        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == exampleTaskName)
            {
                taskRegistered = true;
                break;
            }
        }
    
        if (!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();
    
            builder.Name = exampleTaskName;
            builder.TaskEntryPoint = "PhoneCallBackground.Class1";
            builder.SetTrigger(new PhoneTrigger(Windows.ApplicationModel.Calls.Background.PhoneTriggerType.CallHistoryChanged, false));
                builder.Register();
        }
    }
    

    And don't forget to register it in the appxmanifest file: enter image description here

    The Run Method will be fired after you hang up, if you registered the BackgroundTask. PhoneCallManager can not be registered for BackgroundTask. You need to set it as the default PhoneCall App, if you want to use this.

    Update2:

    I checked the demo, you are still using PhoneTriggerType.CallOriginDataRequest to register the background task. Please change it to PhoneTriggerType.CallHistoryChanged. And also make sure the registered background task is unregistered after you making any change to your codes.

    Here is the demo that I modified: CallHistoryChangeTest.