Activated and deactivated events - windows-runtime

When developing WP 8.1 Windows Runtime application, what’s the equivalent of the PhoneApplicationService.Activated and PhoneApplicationService.Deactivated events? It looks like PhoneApplicationService class is only available in Silverlight WP apps.
Application.Suspending event is not fired on Deactivated event (i.e. when I press Windows button).

You are right, under WinRT there is no Activated/Deactivated events. Your App can be Activated, but it's little different than in Silverlight. About the lifcycle you can read here at MSDN.
Your App will be Suspended just after you Navigate away from it. But - it's not working while you are debugging - more information here. When you run normally your App it will be suspended very fast after you hit Back/Start buttons.
About Navigation there is one thing you should be aware - when you Navigate away from the App, first OnNavigatedFrom is fired, then Suspended events (in the way you have subscribed them). But when you go back to your App - then Resuming events are fired, but OnNavigatedTo is not fired - reference:
Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.
In some situations your App can be put into Not Running state. More again at Lifecycle at MSDN.

Related

What event is raised when the Start button is pressed in Windows Phone 8.1 RT

I used MediaCapture APIs in my App. Let's say I started recording and preview. During recording, I pressed Start key and quickly come back to the App by long pressing Back key and selecting my app (task swap). The result is that the preview is frozen either running on Emulator or 8.1 WP hardware. I don't find a better event to register for Start key. I understand that this.Resume can handle restoration when your APP is being deactivated. But here I mean I switch back from Start within 2~3 seconds and the Resume call is not get called. I am thinking that I can disable preview when Start key is pressed as a workaround. Then when App is brought back to foreground, user can restart recording. However, both scenarios I can't find a proper event. Of course if preview is not frozen when APP is swapped back, I don't need to do anything. Now it seems preview is not working after task swapped back from Start.
Here are the APIs I am using
_mediaCapture.StartRecordToStorageFileAsync(_profile, _recordStorageFile);
capturePreview.Source = App._mediaCapture;
_mediaCapture.StartPreviewAsync();
The issue you're hitting here has to do with the fact that as long as the debugger is attached, suspending/resuming won't work like it will once your app is running on its own.
See here: https://msdn.microsoft.com/en-us/library/windows/apps/hh974425.aspx
Try the same thing you're doing right now, except press the Suspend button in VS once your app is in the background. To bring it back, press the Resume button. Alternatively, try the same thing without the debugger attached.
As long as you're handling the Suspending/Resuming events correctly, MediaCapture lifecycle won't be a problem.
To learn more, have a look at the UniversalCameraSample on the Microsoft github repository: http://aka.ms/2015builduniversalcamerasample. It targets Windows 10, but a lot still applies to 8.1.
There are three events you can register for.
CoreWindow.Activated will be raised when you are no longer the foreground app. This can happen because the user switched away, or because something like a Reminder or Phone Call popped up on top, etc. Simply getting a toast notification does not raise the Activated event.
CoreWindow.VisibilityChanged will be raised when you are no longer visible. This happens when the user switches away, locks the phone, etc.
CoreApplication.Suspending will be raised when you are being suspended. The system doesn't suspend you immediately when the user switches away (in case they switch back quickly, eg they had accidentally hit the Start button). Instead it waits a few seconds before raising this event.

Stopping background audio task on app termination

Due to the way that BackgroundAudio task and app lifecycle are built up, there doesn't seem to be a way to detect actual closing of the app and stopping its background task at this point.
This creates an odd situation for the user, where she can try to forcibly terminate the suspended-but-present app to "get rid" of the playing audio, yet the playback continues.
There is a possibility of reacting to the Suspending event, but that would just mean that the audio stops playing as soon as the user switches out of the app.
However, we are required to stop audio playback only when the user terminates the app via App Switch screen.
Is there any way of detecting that the app has been closed from its background task?
The BackgroundAudioTask is designed to continue even the main app has been terminated/suspended. There is no way to inform the BackgroundTask that the suspended app has been terminated.
After the app is suspended, no events are called or any code is run from your app. The last things what you can do is to put something into suspending event and/or window's events. You can save something in LocalSettings which will be inform the BackgroundTask that the app is inactive, it's also last chance to send a message to your task with information about UI status.
The above method is used in official MSDN sample.

How to subscribe on Activated event in a Windows Phone 8.1

Do anyone know, how to subscribe on Activated event in a Windows Phone 8.1 Universal App?
In Windows Phone 8, I have used:
PhoneApplicationService.Current.Activated +=
WP8.1 Runtime has little different lifecycle - see MSDN. Depending on your needs you can use:
App.Current.Resuming event is called when you resume your app after it had been suspended by the OS - very often, typically few seconds after you navigate away, see also How to Resume an app,
Windows.Current.Activated is called when Window is activated/deactivated - you will have to check event's args. This event doesn't mean that your app had been suspended. You should also look out because this event will be fired for example when you show a dialog (when your window loses focus).
override methods from Application class - after using file pickers, sharing a target and more.
In windows phone 8.1 universal app use following code to subscribe for activated event.
Window.Current.Activated += Current_Activated;
Hope this helps!

Windows Phone 8 App Failed Certification

I recently submitted a very simple app to the WP app store that is basically buttons that play sounds on click.
However, the app failed certification for the following reason(s):
Comments: Result: Fail
The application terminates unexpectedly in response to user input.
Steps to Reproduce:
Play music from music+videos hub and Launch the application.
Tap on any option from MY APPS NAME HERE.
Notice the application unexpectedly terminates.
Comments: Result: Fail
The application terminates unexpectedly in response to user input.
Steps to reproduce:
Play music on the device from the Music + Videos Hub.
Launch the application.
Press the device's universal volume control.
Select "Pause" from the device's universal volume control.
Notice the application unexpectedly terminates.*
My questions are:
What is "the device's universal volume control" ?
How can I stop these errors from happening?
I used Blend to assign a sound to the buttons so there is no code to work with as far as I can see.
I've written similar apps for the Android market and always use a 'stop' and 'release' for each mediaplayer element at the beginning of each button's code. I'm assuming I need something similar here but not sure how to go about it in WP8. Any ideas?
Unviersal volume control refers to the phone physical volume buttons.
Did you have AdMod ads in your app? Recently i found an app with the same behavior failing certification and it was caused by AdMob control doesn't working right when the volume buttons show the volume popup over the app.
If you follow this link, you can see a workaround:
https://groups.google.com/forum/#!topic/google-admob-ads-sdk/vw61ZdJALYo
Hope this helps.

How does my app know if any incoming call coming to phone in Windows Phone 8?

My app is running on the foreground and the phone getting a incoming call. In the scenario, the phone is not raising any event to app like deactivated or closing. How do I identify my app not running ?
You app actually is still the current foreground app (so, kind of running, but audio/video is paused for obvious reasons).
You will get an Obscured event in the app, but you can't distinguish if this due to an incoming call, an incoming SMS, toast or reminder.
Here is a link to the MSDN documentation of the Obscured event you get. There is a corresponding Unobscured event you can also detect.