This app shows how to create a simple Tic-tac-toe game (hence the name "X and Os". After the game loads, you take turns with an AI opponent, placing Xs and Os on the grid until there's a winner (or the board is filled). After each game, the win is tallied at the top of the screen and you have the option to start another game.
While the AI opponent might not be too challenging for all you Tic-tac-toe enthusiasts out there, the interesting part of the sample is how the opponent operates: as a headless process that runs in the background.
You also need to make sure that you build the xandosdroid project before you build and deploy xandos to your BlackBerry device or the simulator. Otherwise xandos won't be able to package xandosdroid into its .bar file.
Running the app in the background
Enabling the app to run in the background is simple. Just modify the entry points of the app, request the user's permission and use the invocation framework to enable the app to be triggered when some content is received. By modifying the entry points in the bar-descriptor.xml file, you can specify that the UI and headless components of your app should use separate entry points.
<configuration name="Device-Debug"> <platformArchitecture>armle-v7</platformArchitecture> <asset entryPointId="XandOs" path="arm/o.le-v7-g/xandos" entry="true" type="Qnx/Elf">xandos </asset> <asset entryPointId="XandOsDroid" path="/arm/o.le-v7-g/xandosdroid" type="Qnx/Elf">xandosdroid </asset> </configuration>
Now you can use the invocation framework to let your app be triggered when any content is received. To do this, you need to specify that your app is a target of type application.headless in the bar-descriptor.xml file.
<invoke-target id="com.example.xandos.droid"> <invoke-target-type>application.headless</invoke-target-type> <invoke-target-name>Headless xandos droid</invoke-target-name> <entry-point-id>XandOsDroid</entry-point-id> <filter> <action>bb.action.START</action> <mime-type>*</mime-type> <property var="uris" value="headless:"/> </filter> <filter> <action>bb.action.CHOICE</action> <mime-type>text/plain</mime-type> </filter> </invoke-target>
Finally, use the
_sys_run_headless system permission in the bar-descriptor.xml file to ask the user's permission to run the app as headless app.
Notice that this is not a long-running headless app. You must use
sys_headless_nostop permission and also submit your application for approval for your app to run as a long-running headless app.
For more information about running apps in the background, check out Headless applications.