firefox-os

Firefox OS: How launch an APP in code


I want to open an APP when one Alarm is fired. I can fire alarms with Alarm API, but I don't know how I can open the APP when the alarm is fired. Now, when the alarm is fired the APP is opened but in background.

I have Firefox OS 1.1.


Solution

  • If the only app you want to launch is your app from inside your app then you can use Open Web Apps API and write code like the following:

    var request = window.navigator.mozApps.getSelf();
    request.onsuccess = function() {
      if (request.result) {
        setTimeout(function() {
          request.result.launch();
        }, 10000);
      } else {
        alert("Called from outside of an app");
      }
    };
    request.onerror = function() {
      alert("Error: " + request.error.name);
    };
    

    The above example will launch (bring in foreground) your App after 10 seconds.

    request.result is an App object which describes your app.


    In case you want to launch other apps, you have to use mozApps.mgmt.getAll() to find other apps (it returns as request.result an array of installed apps - App objects). To use this API your app should be a privileged one. For code examples check at gaia source code which you can find also on github.

    disclaimer: In some cases mozApps.mgmt methods needs your app to be certified, I am not 100% sure if this happens with mozApps.mgmt.getAll(). If someone knows please edit my answer or leave a comment. Thanks!