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.