1

So, this is the problem that I'm trying to solve using background agents: I need to continue on logging the user location when he goes offline in background even after device reset (thing of a running app for context).

To try and solve this I've used a Periodic task from this approach word-for-word, which did work in debug mode. But, as I left the phone unattended overnight I've had only one log for the user after half an hour of leaving the phone. No further entries have been recorded (have in mind that I've used test log entry and not real GPS signal, which means I only triggered simple log writing for test purposes).

Am I missing something? Is this not a valid solution for this problem. If so, could you please provide a reason for this, as I was unable to find any documentation that speaks of that sort of limitation.

Milos Maksimovic
  • 299
  • 2
  • 5
  • 17

1 Answers1

1

The OS on WP 8.1 has some very strict rules about when the background agent will run and for how long. Once every 30 minutes is consistent with the MSDN documentation here and with my observations. If your code is correctly written you should see one entry approximately each 30 minutes. If you were to write a store application for a tablet you'd have more options. There, the OS behavior vis-à-vis of background agents is a little bit more lax (some of this is not very well documented). Since you are targeting Win Phone I am not going to go into details regarding background agents on a tablet.

I am not sure if it would work for your use case but one thing that comes to mind is to have the user keep the app running. You can prevent the phone from going into a low power state and suspend your app (like a navigation app does). To do that see PhoneApplicationService.UserIdleDetectionMode. If you do that then make sure you make the screen black (or mostly black) and use sleeps (await Task.Delay(...)) appropriately in order to conserve the battery.

Ladi
  • 1,274
  • 9
  • 17
  • Thank you for your answer and I did go over those links, however my problem here is that after rebooting the device, API call is made only once after first 30 minutes. After 2 hours of waiting I started debugger again and after this line of code periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask I found that periodicTask.LastExitReason = UnhandledException – Milos Maksimovic Oct 01 '15 at 07:52
  • I see. In that case as you probably know you have a bug. You could place a try/catch at strategic points and log some info to a text file. – Ladi Oct 01 '15 at 08:03
  • Everything custom in OnInvoke method is already in try/catch and writes to log as I said, but the execution doesn't even reach this code after restart. Thanks for your help anyway, I'll keep trying. If I figure out what is happening I will certainly post that here. – Milos Maksimovic Oct 01 '15 at 13:44