由于navigator.mozApps.mgmt.launch方法已不再使用。故用了如下笨方法实现了打开app的方法,要在manifest.webapp中添加permission:"webapps-manage":{},
如下实现的是点击button后打开gallery应用。
var btnLaunch = document.getElementById("launchGallery");
var manifestURL = btnLaunch.dataset.manifestURL; //获取到的值类似于:app://gallery.gaiamobile.org/manifest.webapp
var origin = manifestURL.replace('/manifest.webapp','');
var handleClick = function(e) {
switch(e.target) {
case btnLaunch:
launchApp();
break;
}
};
btnLaunch.addEventListener('click', handleClick);
function launchApp() {
var apps = {};
var appMgr = navigator.mozApps ? navigator.mozApps.mgmt : null;
if (appMgr) {
var request = appMgr.getAll();
request.onsuccess = function onSuccess(e) {
var result = e.target.result;
var HIDDEN_ROLES = ['system', 'input', 'homescreen', 'collection'];
function isHiddenApp(role) {
if (!role) {
return false;
}
return (HIDDEN_ROLES.indexOf(role) !== -1);
}
result.forEach(function(app) {
if (app.manifestURL == manifestURL) { //判断app和要打开的app是否相同
var manifest = app.manifest ? app.manifest : app.updateManifest;
if (!manifest || isHiddenApp(manifest.role)) {
return;
}
var entryPoints = manifest.entry_points;
if (!entryPoints) {
apps[app.origin] = app;
app.launch(); //打开指定app
return;
}
}
});
};
request.onerror = function onError(e) {
console.log(e);
};
}
}