0
votes

I am creating a real time chat application on Windows Phone 8.1 using background task. The app registers background task and triggers it and it's working OK.

My question is how to exchange some run time messages between foreground process and background task ?"

I've seen the API windows.applicationmodel.chat to do the same but this is using Microsoft's API.

How can we implement it using our own set of API?

I mean is there any method to exchange the run time data between foreground process and background task.

1

1 Answers

0
votes

I built a background audio app and the below mentioned sample code from MSDN helped me a lot.

This runs audio files in a Background task and helps us know the events to handle. http://code.msdn.microsoft.com/windowsapps/BackgroundAudio-63bbc319

Also, it will give you a clear idea on how to pass data from foreground task to background task and vice versa.

Eg. from this app: Whenever the background app changes the track it sends message to the foreground app like

 ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, sender.CurrentTrackName);

 if (foregroundAppState == ForegroundAppStatus.Active)
 {
            //Message channel that can be used to send messages to foreground
            ValueSet message = new ValueSet();
            message.Add(Constants.Trackchanged, sender.CurrentTrackName);
            BackgroundMediaPlayer.SendMessageToForeground(message);
 }

Similarly the background app can listen to events from Foreground app:

 void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
    {
        foreach (string key in e.Data.Keys)
        {
            switch (key.ToLower())
            {
                case Constants.AppSuspended:
                    Debug.WriteLine("App suspending"); // App is suspended, you can save your task state at this point
                    foregroundAppState = ForegroundAppStatus.Suspended;
                    ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, Playlist.CurrentTrackName);
                    break;
                case Constants.AppResumed:
                    Debug.WriteLine("App resuming"); // App is resumed, now subscribe to message channel
                    foregroundAppState = ForegroundAppStatus.Active;
                    break;
                case Constants.StartPlayback: //Foreground App process has signalled that it is ready for playback
                    Debug.WriteLine("Starting Playback");
                    StartPlayback();
                    break;
               case Constants.SkipNext: // User has chosen to skip track from app context.
                    Debug.WriteLine("Skipping to next");
                    SkipToNext();
                    break;
                case Constants.SkipPrevious: // User has chosen to skip track from app context.
                    Debug.WriteLine("Skipping to previous");
                    SkipToPrevious();
                    break;
            }
        }
    }

I recommend that you go through the code to understand it better. It took me a while but I must say, its a very good example on MSDN.

Note: This is for Windows Phone 8.1 Runtime apps (not Silverlight)