Current scenario stands like this:
User opens my app
Background audio agent starts playing
User goes back to start screen
Audio keeps playing from the background audio agent
User kills the application from the "task bar" (the closing button in upper right corner)
Background audio agent keeps playing.
I need to achieve following:
Close the audio background agent when the user kills the application (I know that I can use the close method on the backgroundaudioplayer but I need to call this when the application is killed/exited).
Questions:
Is there any event besides: Application_Deactivated / Application_Closing when the user truly exits/kills the application?
If there is no event like mentioned above - can I tell from Application_Deactivated / Application_Closing when the user really exits/kills the application?
Thank you
there is no way to detect when an app got closed by the task switcher or system. Usually apps keep tombstoned until they get either reactivated or killed. You have to do all the clean-up work in the Application_Closing and Application_Deactivated events.
This has one simple reason: When an application lies in the background it is freezed and cannot execute code. Whenever the user or the system kills it (to get more memory for example) the app and all its data get completely wiped out of the memory. Without notifying your app and without giving it the chance to gain performance (which would be counterproductive).
So you just have the chance to use the Application_Deactivated event when the user tap the start button or another app is launched or the Application_Closing event when the user closes the app via back button.
Read this if you need additional information about the Windows Phone 8 Application lifecycle.
Beside this a user would expect that the background audio is still running after he closes the application. A podcast for example should also play when I closed the podcast app. What kind of audio are you talking about? Maybe we can find a smart solution...
The only events you can react to are Closing and Deactivated.
So here,you can call BackgroundAudioPlayer.Instance.Close() in "Application_Closing" event only.
Related
Controlling suspending, resuming and starting Windows Phone Apps with Windows RT, there are 3 events:
OnLaunching -> OnLaunched respectively
OnSuspending
OnResuming
Normally if an app launches, OnLaunching is fired.
When the app suspends e.g. the screen saver comes up, OnSuspending is fired.
If I go back to the app by opening task list, OnResuming is fired.
Now I have the following problem.
If I switch to another app or the screen saver appears, OnSuspending is fired - this is OK.
But if I go back to my app via task list after about 15-20 minutes, the OnLaunching event is fired instead of OnResuming, although my app appears still in the task list.
It's like the app is newly started !
This happens since I redesigned the app in Windows 8.1 RT.
In the earlier version of my app, which was running in Silverlight, this worked fine. Whenever I went back to my app via task list, OnResuming was fired.
Doing some tests, I found out the following:
Launching an app, the previous execution state can be read from the ActivateEventArgs. So I read them out to see, what application status was before launching.
After starting the app the first time, its state is 'notRunning'.
If I close the app and reopen it, the state is 'closedByUser'.
If the app has been suspended and I revoke it after 15-20 minutes, the state is 'terminated'.
However, the state 'terminated' means, the windows has been closed the app.
Why does windows close my app already after 15-20 minutes and why the app is still in the task list if it has been closed ?
Do you have an idea, what the problem is ?
I was told by Microsoft:
The OS can terminate an app in order to free up resources. But such an up can still appear in the task bar.
If the suspended app was terminated, there is no Resuming event and instead OnLaunched() is called with an ApplicationExecutionState of Terminated. So, the routines to restore the app state must be called within OnLaunched().
Sometimes it's required to distinguish if the app has been relaunched by tasklist or by tile. For this, use the TileId property in the OnLaunched(). TileId is "App" if the app has been started by tile and it is empty if the app has been relaunched by tasklist. TileId could also have another value if you generate eg. a second tile for the app giving them another id.
However, this does not work in Windows 8.1 as the value of TileId is always "App" independent of starting it by tasklist or standard tile.
I have implemented voice commands in my Windows Phone 8.1 app, but I have run into 2 problems. The first problem, is that I would like to have the command execute without the app opening. I know it has to load the app to execute, but I'd like it to not be visible, or at least disappear when it is done. The voice command just causes a REST call to be executed and has no UI updating in the process. How can I make it not open/render the view?
Like you stated, it is not really possible.
You could instantly close the app and handle your request in the Suspension event handler using the referral.
But: You will still get the splash screen and is will probably be confusing for the user (why does that thing pop up?). Also, you cannot be sure how much time your suspension gets and if you are on a bad network connection, your rest call may fail.
It might be better to just show a confirmation/progress screen and have the user tab out himself.
Also: Be careful with sayings like just a REST call. If you are on a bad 2G connection or have some other network activity running, even a few kilobytes can take up some time.
Is there a way to know if my App was put into the background, but not actually suspended?
Whenever the user sends your app to the background, it is suspended. To be exact, Windows supposedly wait a couple of seconds to see if the user will immediately switch back before sending the application into Suspended state. Right before this switch occurs, Suspending event is raised to give the application opportunity to save its state in the time period of 5 seconds.
From this point the application can either be reactivated when the user switches back to it or terminated if Windows decides it needs more resources. In either case the application doesn't receive any additional notifications.
Take a look at VisibilityChanged but note the caveat
The visibility event is not serialized with the resume or activation
events. Don't assume that these events come in a particular order.
I am developing an app. Which uses live tiles with background task (as WinRT Component) as I want to updates tile with time trigger. I have noticed that if I set permission of lock screen to ON and let the background task run then app always remains in suspended mode after I close the app. So how can I kill the app and let the background task run on it's own ? I am following this sample.
You can forcefully close an app in a number of ways:
Alt+F4
Swipe the app from the top of the screen to the bottom
Use the task manager
You cannot programmatically kill your app (well you can if you introduce a NullPointerException (DON'T DO THAT) or if system memory requirement kicks in (system needs memory for other tasks so OS will kill your application.).
please read following article on Windows 8 application lifecycle.
Application Lifecycle
I have integrated a small to-do list in my WinForms application, where-in user can add tasks and set alarm for it. Is it possible to run timer (or alarm clock counter) in background, even if application is closed. I am using the AlarmClass written as answer here. The aim is only to show a MessageBox when the alarm time is reached and nothing else to do with the application. Also multiple alarm setting should be possible.
I am sorry if my question is not elaborated, coz I dont know wat other details I must include. But ready to reply your questions.
Thanks in advance.
Edit: Any comments on this link?
Short answer
No. If your application is shut down, there's no good way.
Long answer
Yes if you resort to clever hackery. You could:
Run your app in the system tray / background when the 'X' button is clicked
Run a scheduled task which launches your app in "alarm-checking" mode every N minutes
Write a windows service which launches your app for alarm events
I'm not sure, but can't you create a Windows application which only shows up in the Systemtray. Add the required keys to the registry to make sure it will automatically start when windows starts.
Otherwise you can split it up into two separate applications;
- Windows Application to manage the alert
- Windows Service to do the check in the background