How to switch from foreground application to background agent? - windows-phone-7

I'd like to know if it is possible to switch from foreground app to background agent and vice-versa sharing some data from these two tasks?
More in deep I'd like to do an upload that starts from foreground (Fig. A) and when the user pushes back button the upload continues with background agent (Fig. B).
Image is here: http://imageshack.us/f/823/testter.jpg/

Background file transfer is what you need. It can upload (and download) files while the application is not running:
With Windows Phone OS 7.1, applications are able to queue up one or
more file uploads or downloads over HTTP that will be executed in the
background, even when the application is no longer running in the
foreground. The APIs used for initiating file transfers should be used
to query the status existing transfers and provide progress indicators
for the end user.

You do realize that you can't update the tile constantly from a PeriodicTask right? The minimum refresh time is 30 minutes, unless you do push notifications (which I here, assume you won't, since it's a client-side operation).
And the tile itself doesn't make much sense. And I don't think using a task that can maximum run for 25 seconds makes a aweful lot of sense either for uploading a file.
So I guess we can conclude that it'll not be possible to do this. Instead , you should allow the application to run in the background, by setting the ApplicationIdleDetectionMode to Disabled
PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetecetionMode.Diabled"
Then the user can press the Home button, and the download will progress in the background.

Related

Running settimeout in the background

Why doesn't settimeout/debounceTime work when the app is minimized? Events are still being triggered, but I was wondering if there is a way to make timers work while the app is backgrounded.
As soon user leaves your app, the system will stop it from running to reduce battery usage.
If you like your app to execute in background, you must use the appropriate options in the desired platform (Background Task / Fetch in iOS | Background services in Android).

GPS based application not running under lock screen in windows phone

I have developed a windows phone application and it works fine when it is open and running actively.
It tracks the user movements on the map and shows the position moves with push pins.
Now I want to ensure that the same application runs even the phone is locked when this app was open.
I have added the below code for it to work under lock screen. After this when I verified the app I realized it is not running when the application is locked.
Is there any other change I need to make to get this work under lock screen.Any help would be greatly appreciated.
private void initiliazesettings()
{
PhoneApplicationService.Current.UserIdleDetectionMode =
IdleDetectionMode.Disabled;
}
To have your app running under lock screen on Windows Phone 7.0/7.1/8.0, you need to set ApplicationIdleDetectionMode, not UserIdleDetectionMode. See details here.
Supossing you placed the method in the right place, that code only keeps the screen on, but doesn't make your app run in background when the lock screen is activated. Windows Phone 7 apps cannot run in background, they get tombstoned.
For Windows Phone 8 this is possible - see the sample here from MSDN.
However for Windows Phone 7/7.1 you can't do this.
You can prevent the screen from locking due to the user not interacting with it (as your code shows). However if you manually lock the screen the app will still be made dormant or tombstoned and your location code won't run.
Your only other option is to use a scheduled background task to read the location of the phone, but bear in mind that this is far from real time data (it is a cached location and you only run once every 30 minutes in the best case scenario).

How to deal with Mac OS X Helper/Main app architecture regarding core data, shared preferences and notifications?

I'm having some architectural doubts about a project (Mac OS X app) I'm working on. It basically consists of two elements: a daemon that runs in the background gathering some data and a viewer used to represent the gathered data.
The daemon should be visible in the status bar (no dock icon) and includes a small menu accessible via the status bar. It saves data in a core data store. One of the menu items is a link which opens the viewer. When this viewer is opened, a normal GUI application should start including a dock icon and menubar. The viewer is also opened when opening the application itself (by double-clicking on the icon).
After some experimenting, I figured out the best way to achieve this functionality is by creating two applications, the main application representing the viewer and a helper utility representing the daemon. One of the reasons I did it this way is that it isn't possible to switch between LSUIElement values instantly to force the daemon/viewer state.
Now I have some questions about this architecture:
Both the daemon and viewer application uses the same core data store to save and retrieve data. When having a multi-threaded application I know multiple NSManagedObjectContext objects are needed to correctly synchronize data. What about having multiple applications using the same core data store simultaneously? Is this even possible without having the risk of conflicts, locks, etc.? How do I guarantee consistency?
The daemon should always start when the viewer starts. I achieved this by simply looping through all open processes and checking if the bundle identifier of the daemon is listed. If not, the daemon is started using NSWorkspace's launchApplication. This works fine. Now when the user quits the daemon, the viewer should also stop. What is the best way for the viewer to be notified of the daemon stopping? I can periodically check active processes and quit the viewer if the daemon is gone but that sounds a bit odd. I would rather choose some kind of notification that I'll send when the viewer is about to close. But since this notification should be sent and captured between apps I don't know which simple notification service is available. Any thoughts?
The application is sandboxed as it will be distributed on the Mac App Store. Starting apps with NSWorkspace's launchApplication causes the target app to run in the same sandboxed environment as the source which I think is not a problem at all because running both applications in the same sandbox feels better and probably is. But imagine this scenario: the daemon is started automatically at login (using SMLoginItemSetEnabled) and the user double-clicks Viewer.app. As the daemon is already running (again, this is checked by looping through active processes) it won't be started. Now we have the daemon and the viewer running in different sandboxes right? Will this cause any problems regarding preferences, core data store, etc.?
I would like to use NSUserDefaults for basic configuration, can I somehow interchange this data between the daemon and the viewer? Again, both applications will have different bundle identifiers.
Thanks in advance for your help, appreciated!
There isn't one right answer to this problem, but here's how I'd approach it:
Both the daemon and viewer application uses the same core data store to save and retrieve data.
Because sharing a Core Data store between processes isn't supported (as far as I know), I'd have the daemon expose an XPC Service. Instead of opening the Core Data store itself, the viewer would use an NSXPCConnection to access the data via the daemon.
Assuming the viewer never runs without the daemon, it can use SMLoginItemSetEnabled, like you mentioned in the question, to register a mach service for the daemon, and then connect to that service.
There's sample code that goes over the details of setting that up here on Apple's website (summary: the daemon needs to be at App.app/Contents/Library/LoginItems/daemon.bundle.id.app), and you might also want to read this blog post that discusses some extra requirements imposed by sandboxing (summary: make extra sure that your Team ID is in the daemon's bundle identifier).
The daemon should always start when the viewer starts.
All set: once you register the daemon with SMLoginItemSetEnabled, launchd will start it (if necessary) when the viewer connects to its XPC Service.
Now when the user quits the daemon, the viewer should also stop.
The viewer can use the NSXPCConnection to find out when the daemon quits. The daemon can also use SMLoginItemSetEnabled to un-register itself before it quits, so that it doesn't get relaunched.
I would like to use NSUserDefaults for basic configuration, can I somehow interchange this data between the daemon and the viewer? Again, both applications will have different bundle identifiers.
Use a suite for this:
// To read or write:
NSUserDefaults* suiteDefaults = [[NSUserDefaults alloc] initWithSuiteName:#"com.example.app.shared"];
[suiteDefaults setObject:[NSDate date] forKey:#"launchTime"];
// Add the suite to -standardUserDefaults to make reading easier:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults addSuiteNamed:#"com.example.app.shared"];
To work with sandboxing, the viewer and the daemon must share an App Group. You can even use KVO to observe changes to shared keys.

What happend when I lock my screen and unlock go back to my application?

I found when I lock my phone screen and unlock,The xaml page seems to be reload.This is not the point,the point is the whole program I wrote is just like fresh restart.It is true?What can I do with this issue?
You should read the Execution Model Overview. Your app is getting Tombstoned. You need to handle this.
An application in the foreground can continue to run when the phone screen is locked by setting the PhoneApplicationService.ApplicationIdleDetectionMode property.
By setting up your application to run when the phone screen is locked, a user is able to access the application quickly upon unlock. However, when your application runs under a locked screen, it could consume power outside of the user's control. For this reason, your application must minimize power usage when running under a locked screen

windows phone app running in the background

Is it possible to create an app the runs in the background? If so is there any samples out there for this?
In Windows Phone OS 7.1 you can actually use Background Agents now to perform tasks in the background.
from MSDN:
Scheduled Tasks and background agents allow an application to execute
code in the background, even when the application is not running in
the foreground. The different types of Scheduled Tasks are designed
for different types of background processing scenarios and therefore
have different behaviors and constraints.
You can use a PeriodicTask or ResourceIntensiveTasks. Read more about it in the MSDN article above.
And here's some sample code for you to integrate background agents into your existing app.
Sample Code: Background Agents in Mango
An application in the foreground can continue to run when the phone screen is locked(not background but...) by setting the PhoneApplicationService.ApplicationIdleDetectionMode property. By setting up your application to run when the phone screen is locked, a user is able to access the application quickly upon unlock. However, when your application runs under a locked screen, it could consume power outside of the user's control. For this reason, your application must minimize power usage when running under a locked screen
At the moment there is no way to create application that runs in background.
True multitasking for 3rd party Windows Phone 7 apps will come as an OS upgrade later this year. However, unless the app has to absolutely run in the background (like Pandora etc.), we as developers share some responsibility in making our apps feel at home with the rest of the OS.
Windows Phone OS offers app developers chances to save state of their applications to give the end users the feeling that it never stopped running; this is essentially the same as in other mobile platforms. As your app is being deactivated/closed, you have the option to "Tombstone" your state so that your users can come back to just where they left with BackStack navigation or future launches. Channel 9 had a nice set of demos & labs around tombstoning, found here.
Hope this helps!

Resources