Create A background service in Windows phone 8 to update location - windows-phone-8

I have to create a Background Task which should run after every 1 minute and should call a Rest service to update the Mobile longitude and Latitude Location. The rest service for this purpose have been written all I have to do is to write a Task in my existing application which should perform this update. Can you guys please tell which is the most easiest way to implement this functionality.
Thanks,

There isn't a concept of a service like exists in Windows Desktop. Applications are running, or not on the phone. One application on the phone, tracking location, can be running in the background while other applications run.
If your application is in the foreground, you will just directly call the web services with updated location.
You can use a scheduled background agent to periodically update location, but it is likely that it won't update frequently enough for your needs.
MSDN has details about how to create an application that actively tracks location in the background, subject to some important limitations, and reasons the application may be deactivated:
The app stops actively tracking location. An app stops tracking location by removing event handlers for the PositionChanged and StatusChanged events of the Geolocator class or by calling the Stop() method of the GeoCoordinateWatcher class.
The app has run in the background for 4 hours without user interaction.
Battery Saver is active.
Device memory is low.
The user disables Location Services on the phone.
Another app begins running in the background.
In addition, there is a complete tutorial available for this scenario.

Related

Prevent WinRT App from entering suspend state in a Line-of-Business app

I'm developing a line of business app for Windows 8.1, that is, I am not deploying through the Windows Store and will be able to control all of the features of both the OS and hardware this app is being deployed on.
Because this app is working as the UI in a real-time situation I would prefer if I could ignore the life-cycle events and not have the app suspend or terminate at the whim of Windows 8. Does anyone know of a way to do this?
I have seen some older answers, such as this one and this other one indicating otherwise, but I haven't yet found anything more recently and specifically dealing with the case of a line of business app. I have found the Embedded Lockdown Manager which would prevent the app losing focus and addresses some of the needs I have, but I still would like a way to simple disable Lifecycle events.
Have you tried Assigned Access Mode? Basically use PC Settings -> Accounts to lock an account to a single app. You have to reboot the device and log-in again in order to run anything else.

Run Windows Phone App in background without UI

I am developing a Windows phone 8 app that need to run only in background with UI. Is there any way I can run the app in the background, or without actually being open?
It depends upon what you want to do in the background. Generally speaking you can't implement something like a Windows service that will startup automatically when the phone is launched.
That said you can run your app in the background within given limitations. Check out MSDN for detailed information.
Why all these limitations you might ask yourself? It's to provide a good battery life to the user.
Edit:
For the periodic agent to start running the app must be started once. Further the agent must update a live tile (user must pin it to the start screen) or the app has to be once opened every 14 days.
Another option might be using push notifications to trigger an update..

How does the CurrentApp.ReportProductFulfillment method work?

I'm testing my Windows Phone 8 app for scenarios where my app goes to the background before it can fulfill a consumable purchase. So, on every app-launch I check if there are any unfulfilled in-app purchases, if there are, I fulfill them using the CurrentApp.ReportProductFulfillment method.
However, while testing I noticed this method works even if the device isn't connected to the Internet. So how and when does the app let the Marketplace know that the purchase has been successful? More importantly, should I only do this only if I have an Internet connection?
This is my code by the way :
var licenses = CurrentApp.LicenseInformation.ProductLicenses;
if (licenses["PRODUCT_ID"].IsConsumable && licenses["PRODUCT_ID"].IsActive) {
// Fulfill consumable purchases
// Let the Marketplace know
CurrentApp.ReportProductFulfillment("PRODUCT_ID");
}
Marketplace communications occur on a background task spawned by the OS on regular intervals. You can observe this traffic if you attach Fiddler to a machine running Windows Phone emulator
The following statements are merely suppositions on my part, so take it with a grain of salt. I would imagine the background Marketplace communication handles a number of tasks. The most common of these would be checking for application updates. However, this would also be an ideal time for the OS to communicate fulfillment of an order. In that vein, Marketplace services likely queue your report request and, if it cannot be communicated immediately, defers it for the background task to handle at a later time, allowing the method to run even when the phone does not have data access. Given the nature of modern cashless transactions, I don't see any reason the Marketplace would require immediate notification of fulfillment, as it has time to complete a transaction once initialize authorization is acquired.
CurrentApp.LicenseInformation.ProductLicenses is cached by the windows phone operating system.

Windows Phone 8 - Keeping background location tracking active beyond four hours

I'm in the process of developing a WP8 app that makes use of the background location tracking abilities provided by the OS. The idea is to monitor the users position and to notify them when they are near certain types of places.
So far it all seems to work fine and when running the location tracking works as I would expect.
The problem is, it seems that the phone times out background apps after around four hours, stopping the location tracking.
I can understand why Microsoft did it, to preserve battery life etc. But there's not much point having a background location tracking app that has to be manually restarted every four hours! If a user chooses to run this app and is made aware of the potential battery hit, surely it should be able to run indefinitely - to a point of course, if the system runs out of resources or similar then that's fair enough.
Does anyone have any experience with this? There must be hundreds of others apps in the store that have run into this issue I would have thought? And presumably there must be some way of keeping the location tracking running?
I've tried periodically updating the live tile (using a DispatcherTimer) while the tracking is running but this doesn't seem to be enough to keep the app alive either :(
Anyone have any ideas?
Thanks.
There is no way to achieve your desired behavior. The app will be deactivated under anye of following conditions:
The app stops actively tracking location. An app stops tracking location by removing event handlers for the PositionChanged and StatusChanged events of the Geolocator class or by calling the Stop() method of the GeoCoordinateWatcher class.
The app has run in the background for 4 hours without user interaction.
Battery Saver is active.
Device memory is low.
The user disables Location Services on the phone.
Another app begins running in the background.
Source: Running location-tracking apps in the background for Windows Phone 8
What you could do is to show a toast notification before app is deactivated advising the user, and make him navigate back to the app, extending the period for other 4 hours that way.
There is no way to keep it running without any user interaction.

Callback for phone contacts modification

Does WP8 have any OS hook or callback (sync mechanism) I can implement to get a notification when a contact changes?
For example I'm running an application as a background agent, and a user randomly changes a contact's details. I want to get notified in my background engine so that I can do various operations.
I could always implement a periodic check (say every minute I read all contacts and check to see if there are any modifications - assuming I'm remembering the last configuration somehow). But this solution is not efficient for a large number of contacts, especially when I want the application to run on the low end phones.
Windows Phone does not support any built in way for applications to be notified of contact changes.
As you've noted, the only way to do this would be to track the details and periodically look for differences. Yes, you'd have to do this with the app running (rather than a background agent) if you want to support WP7 devices with one 256MB of memory.
As an alternative and assuming that the user is syncing their contacts with their live.Windows account you could create an external service that periodically polls their live account (via http://msdn.microsoft.com/en-us/live/ff519582.aspx) for changes and then send a notification to the device/app regarding the change.