webapp检测安卓app是否安装并launch
Posted ArgenBarbie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webapp检测安卓app是否安装并launch相关的知识,希望对你有一定的参考价值。
1. cordova插件
1)查看所有已安装的安卓app
https://www.npmjs.com/package/cordova-plugin-packagemanager
A simple plugin that will return a list of installed applications or all on your smartphone. Retuen uid, dataDir and packageName.
function successCallback(e) { alert(JSON.stringify(e)); } function errorCallback(e) { alert(‘Error!‘ + e); } window.plugins.packagemanager.show(true, successCallback, errorCallback);
2)检测某安卓app是否已安装
https://www.npmjs.com/package/cordova-plugin-appavailability
从1)返回的list of installed applications中找到所需app的scheme,例如高德地图:com.autonavi.minimap。
var scheme; if (device.platform === ‘ios‘) { scheme = ‘iosamap://‘; } else if (device.platform === ‘android‘) { scheme = ‘com.autonavi.minimap‘; } appAvailability.check( scheme, // URI Scheme or Package Name function () { // Success callback alert(scheme + ‘ is available :)‘); }, function () { // Error callback alert(scheme + ‘ is not available :(‘); } );
3)launch app
https://github.com/nchutchind/cordova-plugin-app-launcher
window.plugins.launcher.launch( { uri: ‘androidamap://route?sourceApplication=cortanademoservice&slat=39.979366&slon=116.31028&sname=我的位置&dlat=39.980016&dlon=116.326568&dname=公安局&dev=0&t=0‘ }, successCallback, errorCallback );
2. javascript的tricky实现方法
安装app的情况下,网页会进入后台打开app,网页进入后台后会挂起js的执行,但是这个期间有600-1000ms的时间js仍然会执行,可以利用打开app的延时来判断是否有该app。
var t = 1000, hasApp = true; setTimeout(function () { if (!hasApp) { cordova.InAppBrowser.open(‘http://m.amap.com/navigation/carmap/saddr=116.310322%2C39.978957%2C%E5%BE%AE%E8%BD%AF%E5%A4%A7%E5%8E%A6&daddr=116.326568%2C39.980016%2C%E4%B8%AD%E5%85%B3%E6%9D%91%E6%B4%BE%E5%87%BA%E6%89%80&saddr_lonlat=116.310322%2C39.978957%2C%E5%BE%AE%E8%BD%AF%E5%A4%A7%E5%8E%A6&daddr_lonlat=116.326568%2C39.980016%2C%E4%B8%AD%E5%85%B3%E6%9D%91%E6%B4%BE%E5%87%BA%E6%89%80&saddr_typecode=120201&daddr_typecode=130501&saddr_poiid=B0FFFSPTNE&daddr_poiid=B000A7FCSP&maddr=&sort=&addPassing=remove‘); } $("#ifr").remove(); }, 2000); var t1 = Date.now(); var ifr = $(‘<iframe id="ifr"></iframe>‘) ifr.attr(‘src‘, ‘androidamap://route?sourceApplication=cortanademoservice&slat=39.979366&slon=116.31028&sname=我的位置&dlat=39.980016&dlon=116.326568&dname=公安局&dev=0&t=0‘); $(‘body‘).append(ifr); setTimeout(function () { var t2 = Date.now(); //delay time 30 may vary for different type of phones if (!t1 || t2 - t1 < t + 30) { hasApp = false; } }, t);
利用iframe的动态加载来尝试打开app。
以上是关于webapp检测安卓app是否安装并launch的主要内容,如果未能解决你的问题,请参考以下文章
下载安卓app软件并安装,如何在Android系统手机上安装软件的三种