Search code examples
c#windows-phone-8

Event occured when phone receives call


I am developing an Windows Phone Application and I waned to know that when some page is showing on the screen and the phone receives a call, which event occurs? How do I subscribe to that event?

I tried checking application deactivating event and RunningInBackground event but it is no getting fired. Its silly question but could not find anything understandable on internet. Is it page level or application level?


Solution

  • You can use the Obscure and Unobscured events, which occur when the Windows Phone Shell covers your application window.

    What does this mean? When on windows phone you get a call, your application window gets covered by the "incoming call view". When an alarm in your phone sets off, a view that covers 30% of the screen appears, covering your app view partially.

    These views fire the obscured event on your application.

    From the MSDN:

    There is no indication of which piece or pieces of the shell chrome are obscuring the application. An application will get the Obscured event immediately after coming to the foreground if there is already some UI covering the screen. However, the event is not raised when the application is navigated away from during usage.

    You can get more info here:

    Obscured event

    Unobscured event

    And finally, how to use them with a simple example that can only be run on a device:


    MainPage.xaml

    <TextBlock Name="txtObs" Grid.Column="0" Margin="0,10,0,0" Grid.Row="2"/>
    <TextBlock Name="txtUnobs" Grid.Column="0" Margin="0,80,0,0" Grid.Row="2"/>
    

    MainPage.xaml.cs

    In the constructor add:

    PhoneApplicationFrame rootFrame = (Application.Current as App).RootFrame;
    rootFrame.Obscured += OnObscured;
    rootFrame.Unobscured += Unobscured;
    

    And then outside of it:

    void OnObscured(Object sender, ObscuredEventArgs e)
    {
       txtObs.Text = "Obscured at "  + DateTime.Now.ToString();
    }
    
    void Unobscured(Object sender, EventArgs e)
    {
       txtUnobs.Text = "Unobscured at "  + DateTime.Now.ToString();
    }
    

    One thing to keep in mind is that in order to run your application in locked mode ApplicationIdleDetectionMode needs to be disabled:

    PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
    

    Try it out!