http://my.oschina.net/liucundong/blog/354029

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

目录[-]

  • 实现效果
  • 实现方式
  • HTML代码
  • AndroidMainfext.xml

之前写过一篇blog,介绍如何通过点击手机浏览器中的链接,直接打开本地Android App。

实现方式不太完美,最近看了微博、京东的手机版网页,感觉他们的实现方式很不错,研究了一下,实现以下效果:

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

实现效果

如下图所示,在手机浏览器中访问京东的手机版网站(m.jd.com),顶部会有一个广告图,点击这个广告图,如果手机上已经安装了京东App,则直接打开,如果没有安装,则开始下载。

实现方式

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

<dataandroid:host="splash"android:scheme="cundong"/>

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

varifr=document.createElement('iframe');ifr.src="cundong://splash"

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

window.location=download_url;

HTML代码

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

AndroidMainfext.xml

<activityandroid:name=".activity.LauncherActivity"android:configChanges="orientation|keyboardHidden|navigation|screenSize"android:label="@string/app_name"android:screenOrientation="portrait"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter><intent-filter><actionandroid:name="android.intent.action.VIEW"/><categoryandroid:name="android.intent.category.DEFAULT"/><categoryandroid:name="android.intent.category.BROWSABLE"/><dataandroid:host="splash"android:scheme="cundong"/></intent-filter></activity>

更多相关文章

  1. android GridView选择照片的完整实现
  2. Android中实现循环滚动
  3. Android(安卓)Studio如何集成Genymotion
  4. android从相册中选择报无法加载照片
  5. 如何POST一个JSON格式的数据给Restful服务
  6. 为 Android(安卓)添加 Java 层服务(三)
  7. 解决方法:Eclipse的 window-->preferences里面没有Android选项
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Pycharm安装PyQt5的详细教程

随机推荐

  1. android学习小结3
  2. Android关于SD卡中多层目录的创建
  3. Android Logo消息角标数字提醒
  4. Android 网络连接-重试机制-HttpRequestR
  5. android启动时自动抓取logcat
  6. Android世界的Swift - Kotlin语言
  7. android 锁屏页
  8. android聊天表情开发
  9. android 图表引擎AChartEngine(柱状图)
  10. android 网络判断的几种方法