如何从网页中检测Android设备上是否安装了应用程序

时间:2011-07-04 06:10:28

标签: javascript android detect

情况如下:

如果我的应用已安装在当前运行的Android设备上,我有一个需要通过JavaScript检查的网页。

如果安装了该应用,该页面将显示一个链接(使用自定义协议)来启动该应用, 否则页面应该显示到Android市场的链接。

我可以管理应用和市场的链接。剩下的唯一步骤是从JavaScript代码中检测设备上应用程序的存在(或者尝试捕获不支持的协议的可能错误,作为不存在应用程序的指示)。

当我

  1. 点击带有
  2. 的网络链接
  3. 我的自定义应用协议和
  4. 该应用尚未安装在设备上
  5. 我可以看到android环境生成“不支持协议”类型错误。 不幸的是,我无法在JavaScript代码中捕获该错误以将用户转移到Android市场。

    我想直接检测和错误检测都是有效的方法,如果它们存在的话。

    任何想法我怎样才能做到这一点?

    感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

请勿使用自定义协议。

相反,请设置<intent-filter>以查看知名网址(例如,设置方案,主机和路径)。该网址应指向“下载应用”说明所在的页面。

然后,如果用户点击来自其他地方的知名网址链接,则会在安装后启动您的应用,否则会启动您的网页。

答案 1 :(得分:2)

我也有同样的问题,但我这样解决:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) { // if is android
    // your app address for local
    var ifrSrc = 'intent://example.com#Intent;scheme=my_scheme;action=android.intent.action.VIEW;end';
    var ifr = document.createElement('iframe');
    ifr.src = ifrSrc ;
    ifr.onload = function() { // if app is not installed, then will go this function
        window.location = 'http://your.app_download_page.com';
    };
    ifr.style.display = 'none';
    document.body.appendChild(ifr);

    setTimeout(function(){
        document.body.removeChild(ifr); // remove the iframe element
    }, 1000);
} else { // if is not android
    window.location = 'http://google.com';
}

希望这可以帮助某人解决这个问题。

答案 2 :(得分:0)

您可以使用iframe,其中src url是您应用的自定义协议,同时如果未处理自定义协议,您可以显示您的网页。您还可以使用frameborder =“0”和width and height = 0将iframe设置为invisble;