HTML5打开本地app应用的方法,如果你的手机上安装App,就会自动打app,否则在页面上会提示你进行下载app
2016-06-14 17:31
1011 查看
html中其实是无法判断应用是否安装,除非在webview中通过js bridge,这里通过一种方式达到此目的。
1、编辑AndroidManifest.xml:
主要是增加第二个,myapp用来标识schema,最好能保证手机系统唯一,那样就可以打开应用,而不是弹出一个选择框。
android:pathPrefix标识url的path,可以附带自己的数据通过string传递到activity,比如完整url为 myapp://xxx/openwith?data=mydata
[html] view plaincopy
然后通过activity获得data数据:
[java] view plaincopy
2、编写html页面:
整个页面也许是某个app的详细介绍,这里只写出关键的js代码:
[javascript] view plaincopy
上面代码可以达到这样一个目的,先请求 myapp:// ,如果系统能处理,或者说已经安装了myapp表示的应用,那么就可以打开,另外,如果不能打开,直接刷新一下当前页面,等于是重置location。
参考前辈的:文章链接如下:
http://www.tuicool.com/articles/uQr6Fff
http://www.jb51.net/html5/446382.html
http://www.cnblogs.com/yejiurui/p/3413796.html
http://zhidao.baidu.com/question/809150834793726172.html
1、编辑AndroidManifest.xml:
主要是增加第二个,myapp用来标识schema,最好能保证手机系统唯一,那样就可以打开应用,而不是弹出一个选择框。
android:pathPrefix标识url的path,可以附带自己的数据通过string传递到activity,比如完整url为 myapp://xxx/openwith?data=mydata
[html] view plaincopy
<activity android:name="com.abc.MainActivity" android:configChanges="orientation|keyboardHidden|navigation|screenSize" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="myapp" android:pathPrefix="/xxx/openwith" /> </intent-filter> t;/activity>
然后通过activity获得data数据:
[java] view plaincopy
public void onCreate(Bundle savedInstanceState) { Uri uridata = this.getIntent().getData(); String mydata = uridata.getQueryParameter("data"); ... }
2、编写html页面:
整个页面也许是某个app的详细介绍,这里只写出关键的js代码:
[javascript] view plaincopy
function openApp() { if (/android/i.test(navigator.userAgent)) { var isrefresh = getUrlParam('refresh'); // 获得refresh参数 if(isrefresh == 1) { return } window.location.href = 'myapp://xxx/openwith?data=mydata'; window.setTimeout(function () { //window.location.href = '下载链接' ; window.location.href += '&refresh=1' // 附加一个特殊参数,用来标识这次刷新不要再调用myapp:// 了 }, 500); } }
上面代码可以达到这样一个目的,先请求 myapp:// ,如果系统能处理,或者说已经安装了myapp表示的应用,那么就可以打开,另外,如果不能打开,直接刷新一下当前页面,等于是重置location。
参考前辈的:文章链接如下:
http://www.tuicool.com/articles/uQr6Fff
http://www.jb51.net/html5/446382.html
http://www.cnblogs.com/yejiurui/p/3413796.html
http://zhidao.baidu.com/question/809150834793726172.html
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件