2

I have Android application where service running 24/7 Phone get's GPS position every 5 minutes and sends to server. This is requirement.

Can I write same service for iPhone? I'm not sure if it's possible.

Can I write same service for WP7? I think it wasn't possible to run service before. Did anything change in 7.5?

EDIT: I'm not sure why somebody downvoted. This is concrete Yes/No question.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
katit
  • 17,375
  • 35
  • 128
  • 256

4 Answers4

6

It is possible in WP7 with the introduction of background tasks in Mango. In fact, there is a built in mechanism for polling the GPS provided by the API that is more battery efficient.

Not sure if it goes down to 5 minutes, think it is something larger like 30 minutes. It is also not comparable to a Windows Service - so don't go fowards with that mind set. Background tasks are heavily constrained to keep the phone responsive for the user - to make use of them, you need to play nice with the requirements.

Background tasks introduction, it also talks about the GPS thing I mentioned. The entire series is well worth your bandwidth and time downloading and watching:

http://channel9.msdn.com/Series/Mango-Jump-Start/Mango-Jump-Start-06-Windows-Phone-Multi-tasking--Background-Tasks

Can't answer for iPhone.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • Can you elaborate a little? When GPS polled, can I run my code or it goes to some storage until app visible? I need to immediately send those GPS positions to server. Possible? – katit Oct 19 '11 at 13:46
  • Yes possible, learn about background tasks. However, you will find you are forced to operate under strict conditions (little processing power, short about of time) in order for the phone to maintain the user experience. – Adam Houldsworth Oct 19 '11 at 13:46
  • Adam Thank you. I wish this was possible 8 month ago when I wrote Android app. 30 minutes is still too much for my particular need. Is there any way to simulate smaller span? Maybe having multiple tasks that run at different times? – katit Oct 19 '11 at 13:52
  • You aren't going to achieve a fast polling mechanism, and you can only register one task I think per app: http://blogs.infosupport.com/blogs/alexb/archive/2011/05/26/multi-tasking-in-windows-phone-7-1.aspx – Adam Houldsworth Oct 19 '11 at 13:56
  • That makes perfect sense. I see it gives 15 seconds to complete task. This won't even be enough to obtain GPS position. Can GPS be separated and this 15 seconds used only to send update to server? – katit Oct 19 '11 at 14:39
  • I think one app can only expose one type of each background task. So you could do the GPS poll in the standard 15 seconds (should be enough as WP caches the GPS stuff internally and exposes a copy for fast access), and then use the Background File Transfer task to update your server. – Adam Houldsworth Oct 19 '11 at 14:45
1

It is also possible on ios4+ but it wont be time-triggered. Either you register for precise (gps) or vague location (wifi and wan location) which is available to get in background but it is not always possible to send that data to a server because after 10 minutes in background your app is not allowed to keep a network-connection alive. So you have to buffer that data and have to wait until the user launches your app.

The ios pushes notifications to your app depending on the needed accuracy and depending on a distance-filter

ios-apps do not differ between services and activities (like in android). it is all combined in one app.

thomas
  • 5,637
  • 2
  • 24
  • 35
  • So, app like on my website basically not possible with iOs? – katit Oct 19 '11 at 13:54
  • it is. tracking is possible but sending live-traffic is only possible in a reliable way if the app is "open" (not in background) – thomas Oct 19 '11 at 13:56
  • I understand it's not possible than. In my case - driver uses app maybe once per day to send message or get load information. But office need to see where he is all the time. So, app closed most of the time (90% of the time). Do I understand correctly that I can't send updates to server after about 30 minutes of last app use? – katit Oct 19 '11 at 14:31
  • Another note. In Android I don't keep HTTP connection. I open it every time I do update (every 15 minutes) – katit Oct 19 '11 at 14:37
  • I think it is 10 minutes after app-close that this app is not allowed to run any network-traffic: neither open a new connection nor holding a connection open. So live-updates wont be possible (reason: saving battery-energy). But maybe there are some tricks (but then it will be hard to get into the app-store I guess) – thomas Oct 19 '11 at 17:53
  • Here they say it is doable... http://stackoverflow.com/questions/6857491/posting-location-updates-in-background-from-ios4 – katit Oct 19 '11 at 18:06
  • it is doable but in my point of view not reliable. it is ok for most applications which only send some last packages to a server but I think it runs fine for an hour perhaps and then stops sending. Please read the discussion-bullet of the documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler: and maybe try it out. The app is not difficult to code (a prototype can be easily done in a workday) – thomas Oct 19 '11 at 18:23
  • Unfortunately, to me it is hard to prototype as I need to get Mac, iPhone and learn from beginning :) I need to know if it's doable or not. There no apps like this which tell me it would be hard.. – katit Oct 19 '11 at 18:27
  • I would say that live-traffic in background is doable but not reliable and you also need the reliability because you want other to pay for this. (not sure if sth. changed in ios5 but I dont think so). more I cannot say. – thomas Oct 19 '11 at 18:35
0

There are "some limitations"(a lot) here too, but maybe Microsoft Push services works for you:

Push Notifications Overview for Windows Phone

Because polling is not a good practice for this, push works better in this cases.

yeradis
  • 5,235
  • 5
  • 25
  • 26
0

In WP7 Mango you can't get a fresh location from a background agent. The following code will return the latest available location (up to 15 minutes old):

private GeoPosition<GeoCoordinate> GetCachedLocation()
{
    GeoCoordinateWatcher geoWatcher;
    geoWatcher = new GeoCoordinateWatcher(); //Start a new watcher with default level of accuracy
    geoWatcher.Start();
    //Get latest cached position
    GeoPosition<GeoCoordinate> position = geoWatcher.Position;
    geoWatcher.Stop();
    return position;
}

As you can see, this uses GeoCoordinateWatcher. According to MSDN (http://msdn.microsoft.com/en-us/library/hh202962(v=vs.92).aspx):

This API, used for obtaining the geographic coordinates of the device, is supported for use in background agents, but it uses a cached location value instead of real-time data. The cached location value is updated by the device every 15 minutes.

Other than that, I haven't been able to find much information. You could create a GeoCoordinateWatcher with a self-defined accuracy, but I haven't tested this. Perhaps it would return the latest available cached location that satisfies the accuracy requirement.

Anders
  • 1,401
  • 3
  • 16
  • 20