通过浏览器直接打开Android应用程序

需求

通过手机浏览器直接打开Android应用程序。如果本地已经安装了指定Android应用,就直接打开它;如果没有安装,则直接下载该应用的安装文件(也可以跳转到下载页面)。


实现效果

如果手机上已经安装了App,则直接打开,如果没有安装,则开始下载。

实现方式

1.为Android应用的启动Activity设置一个Schema,如下:

[html]  view plain copy
  1. <data android:host="splash" android:scheme="huiyy" />  


2.用户点击浏览器中的链接时,在动态创建一个不可见的iframe,并且让这个iframe去加载步骤1中的Schema,如下:

[html]  view plain copy
  1. /*scheme:必须*/  
  2. //scheme_IOS: 'huiyy://',  
  3. //scheme_Adr: 'huiyy://splash',  
  4.   
  5. var ifr = document.createElement('iframe');  
  6.    
  7. ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS : config.scheme_Adr;  


3,如果在指定的时间内没有跳转成功,则当前页跳转到apk的下载地址(或下载页),如下:

[html]  view plain copy
  1. window.location = download_url;  

HTML代码

[html]  view plain copy
  1. <!doctype html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.    
  6.         <meta name="apple-mobile-web-app-capable" content="yes">  
  7.         <meta name="apple-mobile-web-app-status-bar-style" content="black"/>  
  8.    
  9.         <title>this's a demo</title>  
  10.         <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui">  
  11.     </head>  
  12.     <body>  
  13.         <div>  
  14.             <a id="call-app" href="javascript:;" class="label">下载客户端>></a>  
  15.             <input id="download-app" type="hidden" name="storeurl" value="http://www.baidu.com">  
  16.         </div>  
  17.    
  18.         <script>  
  19.             (function(){  
  20.                 var ua = navigator.userAgent.toLowerCase();  
  21.                 var t;  
  22.                 var config = {  
  23.                     /*scheme:必须*/  
  24.                     scheme_IOS: 'huiyy://',  
  25.                     scheme_Adr: 'huiyy://splash',  
  26.                     download_url: document.getElementById('download-app').value,  
  27.                     timeout: 600  
  28.                 };  
  29.    
  30.                 function openclient() {  
  31.                     var startTime = Date.now();  
  32.    
  33.                     var ifr = document.createElement('iframe');  
  34.    
  35.                     ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS : config.scheme_Adr;  
  36.                     ifr.style.display = 'none';  
  37.                     document.body.appendChild(ifr);  
  38.    
  39.                     var t = setTimeout(function() {  
  40.                         var endTime = Date.now();  
  41.    
  42.                         if (!startTime || endTime - startTime < config.timeout + 200) {   
  43.                             window.location = config.download_url;  
  44.                         } else {  
  45.                                
  46.                         }  
  47.                     }, config.timeout);  
  48.    
  49.                     window.onblur = function() {  
  50.                         clearTimeout(t);  
  51.                     }  
  52.                 }  
  53.                 window.addEventListener("DOMContentLoaded", function(){  
  54.                     document.getElementById("call-app").addEventListener('click',openclient,false);  
  55.    
  56.                 }, false);  
  57.             })()  
  58.         </script>  
  59.     </body>  
  60. </html>  


AndroidManifest.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.downappdemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="19" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.downappdemo.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.             <intent-filter>  
  25.                 <action android:name="android.intent.action.VIEW" />  
  26.   
  27.                 <category android:name="android.intent.category.DEFAULT" />  
  28.                 <category android:name="android.intent.category.BROWSABLE" />  
  29.   
  30.                 <data android:host="splash" android:scheme="huiyy" />  
  31.             </intent-filter>  
  32.         </activity>  
  33.     </application>  
  34.   
  35. </manifest>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值