QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Intel XDK Programming Guide to Background Execution

Intel XDK Programming Guide to Background Execution

background-execution

This post is a part 4 of Intel XDK Complete Tutorial post series.

One of the most confusing and tough topics of mobile application development is executing code and accessing hardware components like Microphone, Bluetooth, GPS, Speaker etc while app is in background state.

In this tutorial I will provide ultimate guide to run code in background state and also execute APIs of hardware components.

States in Nutshell

An app can only be in one state out of five states in a particular time. Five states are: Background State, Resume State, Screen Lock State, Continue State and Running State. In my previous article I explained what’s the difference between these states and how to detect change in these states.

In this tutorial I will concentrate on how to run code when app is in background or screen lock state. Some developers call these two states with one name i.e., suspended state.

App Behaviour in Suspended State

An app is said to be in suspended state when user presses home button, switches to some other app or locks the screen. In suspended state mobile operating system stops providing any CPU time to the app and blocks access to programmable hardware components like GPS, speaker, microphone, camera etc.

But if you app needs to run code on suspended state or access hardware components in suspended state then mobile operating systems provide an explicit way to request permission specific to the task and hardware component.

Hardware components like filesystem(storage device) and internet(Wifi or Cellular) are available whenever you have permission to run code in suspended and/or access any hardware components.

Running Task in Suspended State

If you app is executing a very important task in that case you can keep your app running the task even after app is suspended using Backround Mode plugin. You can only run code(or task) but you won’t have access to hardware components using this plugin.

This can be useful when your app is doing some huge mathematical calculation and its taking some time. User’s usually suspended the app and resume/continue it later on. So you can completed the calculation on suspended state.

You can run the task for as long as you want.

Playing Sound on Suspended State

If you want to play music on suspended state then you need permission to run code on suspended state and also access to speakers on suspended state. For this you can use Background Audio plugin.

You can play music for as long as you want.

Tracking User Location on Suspended State

If you want to access GPS on suspended state then you need to permission to run code on suspended state and also access to GPS device on suspended state. For this you can use Background Geolocation plugins.

Downloading on Suspended State

Although while using the Background Mode plugin you are allowed to download files on suspended state you shouldn’t use it to download. That’s because due to excess usage of Internet bandwidth and conflicting bandwidth with other apps your app will get terminated. So for downloading large files on suspended state use Background Download plugin.

Final Thoughts

There are many other plugins for accessing other hardware components on background state. Understanding states and how to crack them down will help to make you a great Intel XDK developer. Intel XDK legacy apps don’t support these third-party plugins therefore you are unlucky if you are planning to create a legacy app. But legacy apps does provide couple of options while creating app binary for including permissions to run code on background and access only couple of hardware components.

Caution

Be caution while using these plugins. You should consume minimum network bandwidth and CPU time. If you app is trying to exhaust the OS on suspended state then it can terminate the app. Therefore on suspended state do minimal and important tasks.

Jan 11, 2015Narayan Prusty
Sending Data to Server on Page UnloadCreate a Feed Reader App using Intel XDK
Comments: 6
  1. Rodrigue
    6 years ago

    How to connect and print the data in INTEL XDK with Bluetooth printer

    ReplyCancel
  2. Massimiliano B.
    7 years ago

    Hi Narayan,

    I have been conducting a scouting of different cross-platforms environments regarding a few specific needs I have. Among them, Corona, Unity, and had looked at XDK in the past but am now reevaluating it.

    You clearly have a lot of experience with XDK – I wanted to ask, according to your experience:

    0) I am not sure what you mean by your background plugin not accessing the hardware, this might already answer the questions below …

    1) is it possible with XDK to handle geofences for Android and iOS?

    Specifically:
    – can I add / modify / remove a geofence (either from running or background state)? As far as I know any app able to call i.e. google play services should be able to manage geofences. Would XDK + maybe your background plugin allow me to handle that in iOS / Android?
    – Geofences are circular in shape in both iOS and Android. Can I – should an user, say, enter a geofence – call some code in my app to do some more refined checks about it? e.g. If an user enters a geofence, I want to run a routine to check if he’s into a rectangle that is inscribed in that geofence and for which the round approximation is not good enough;
    – let’s suppose the user above entered the geofence and is dwelling in it. Can I somehow periodically do the “in-rectangle” check above(either programmatically maybe with your background plugin, or how does the OS handle the “dwell” state and informs the XDK geofence-registered app?)

    2) Does XDK allow usage of the bluetooth? does it work in suspended state (maybe with your plugin?)

    3) Oh, this one is easier to answer … :-) when I evaluated XDK a year or more ago, I was put off by the difficulty in making an app look good and the text readable. Corona has a very good content scaling system. I was wondering if I could just start from some responsive template such as a bootstrap one and use it as the basis of a medium complexity but tidy UI. what do you think?

    Many thanks for your answer!

    ReplyCancel
  3. Rexda
    7 years ago

    With the Background Mode Plugin, is it possible to perform background upload process? or Is there any background upload plugin?

    ReplyCancel
  4. Nalbi
    7 years ago

    Thanks for your awesome set of tutorials, it helps me a lot.

    Will an application running in background with a 3rd party Cordova plugin be more likely rejected by the AppStore / Google Play at submission? (Checking URL every N minutes and trigger notification in case of change)

    ReplyCancel
  5. Thalvez
    8 years ago

    With the Backgound Mode Plugin, can i know if the device have internet connection when the processes is stopped?

    ReplyCancel
    • Narayan Prusty
      8 years ago

      Yes you can.

      ReplyCancel

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

Image8 years ago 6 Comments Cordova
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • States in Nutshell
  • App Behaviour in Suspended State
  • Running Task in Suspended State
  • Playing Sound on Suspended State
  • Tracking User Location on Suspended State
  • Downloading on Suspended State
  • Final Thoughts
  • Caution
Related Articles
  • Intel XDK Orientation Guide
  • Record Video using Intel XDK
  • Create a Camera App using Intel XDK
  • Share Button for Intel XDK APP
  • Intel XDK App using WordPress Backend
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license