Background widget

Hi,

I have some questions about background processing: I want to design a widget which collects data on a hourly basis. What happens to the background service if the widget is started by user? Will it start another background service which mewns I have to kill the previous?

What happens with the background service if its triggered but currently the widget ie displayed? Will it executed anyway?

  • Arrgggh, now it works. You were totally right.. Thanks for that. Just a couple of other questions: I want to start the background servce every full hour. Therefore I would use this code to do the Backgrounding:

           		var min = System.getClockTime().min;
           		if (min > 55) {
           			// save a value directly since next backgrounding is possible 
           			// in > 5 minutes only
         			Background.registerForTemporalEvent(new Time.Duration(120-min));
           		} else {
         			Background.registerForTemporalEvent(new Time.Duration(60-min));
           		}
    

    If the widget is running during a full hour and a new background value is been pushed in, I have to call onBackgroundData(data) manually. Did I get this right?

    And my third question: I would like to know the time and the battery status when the charger is pulled. Is this possible anyhow with backgrounding? I didn't found an event to detect this.

  • Ok, one thing to consider here.  No matter how you schedule the temperal event, there's no guarantee it will run at that specific time.  Let's say you set the time for exactly xx:00, but start a CIQ device app 10 minutes before that and use it until 10 minutes after that.  Your background will run at xx:10.  So in your background process when it's ending, you'll want to set the temporal event for 50 minutes to run at xy:00, but again, it might not run right at that time....

    And then there's the case where you're running a device app for two hours and may miss a whole hour.

    As far as charging, you can check for that in the background (Sys.Stats.charging), but that really isn't charging - it's "connected to external power", as it will stay true even when the battery is at 100%.  And there's no event that's triggered by this.

  • Hi Jim,

    I experimented even further and currently I am wonderin why one would need the Background Service Delegate if the App class (and implicitly the getInitialView) is called anyway. I could put everything in the getInitialView since it is called anyway on each temporal background event, isn't it?

  • There is no view in a background task. getInitialView isn't called when the background runs

  • ok, I see. But the onBackground is called, isn't it? At least in your example app you do that stuff (store in the object store) in the app in onBackground(data).

  • In appBase, there are things called only in the main app, things called only by the background, and things called by both.

    initialize(), onStart(), onStop() are examples of some called by both

    onBackgroundData() and getInitialView() are examples what's only called in the main view, while   getServiceDelegate, only by the background()

  • Puh, that's pretty complicated. I don't understand then why you have the OS storage of the data in onBackground(data) if this is called only in the main view. I would expect to have this in the service delegate....

  • Hi again,

    I am experimenting again but need again your help. I have following in my background process:

        function onTemporalEvent() {
           	var data = Background.getBackgroundData();
           	Sys.println("onTemporalEvent on Entry: " + data);
            var stats = Sys.getSystemStats();
           	if (stats.charging) {
            	if (data == null) { data = {}; }
           		data.put(:batAtChargeUnplug, stats.battery);
           		data.put(:timeAtChargeUnplug, getTimestamp());
           	} else {
           	
           	}
            Sys.println("on TemporalEvent: " + data[:batAtChargeUnplug]);
            Background.exit(data);
        }
    

    but as soon as I fire a temporal event from the simulator (battery is charging), I get following error message, although exit method supports dictionaires:

    Error: Unexpected Type Error
    Details: Failed invoking <symbol>
    Stack:
    - onTemporalEvent() at ...\bgbg.mc:31 0x1000023e

    Furthermore testing is complicated since the widget is running and data is immediatly transferred to the main process. So I can't test that adding data to the dictionary is working properly. Is there any workaround for that?

  • What is line 31 in your code?  which SDK?

  • Line 31 is the Background.exit(data); line. The SDK is 3.1.3