首页 技术 正文
技术 2022年11月15日
0 收藏 801 点赞 2,792 浏览 3948 个字

Android开发——通过扫描二维码,打开或者下载Android应用

 

在实现这个功能的时候,被不同的浏览器折磨的胃疼,最后实现了勉强能用,也查考了一下其他人的博客

android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据

android/iPhone:如何从browser直接打开应用程序或者打开应用商店(如果没有应用程序)

1、Html页面(JS不在行,这个是其他人写的)

需要留意的是Android_URL,格式需要符合[scheme]://[host]/[path]?[query]

scheme:判别启动的App。

host:适当记述

path:传值时必须的key (没有也可以)

query:获取值的Key和Value (没有也可以)

1 Android_URL = "myapp://www.test.com/openwith?uid=123";
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta content="telephone=no" name="format-detection" /><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />    <title>打开或下载应用</title>     <script src="assets/plugins/jquery-1.8.3.min.js" type="text/javascript"></script></head><body> <a id="vlink" onClick="try_to_open_app()" style="display:none"></a><script>var browser={            versions:function(){                        var u = navigator.userAgent, app = navigator.appVersion;               return {                            trident: u.indexOf('Trident') > -1, //IE内核                            presto: u.indexOf('Presto') > -1, //opera内核                            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核                            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核                            mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否为移动终端                            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端                            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器                            iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器                            iPad: u.indexOf('iPad') > -1, //是否iPad                            webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部               };    }()    var iOS_URL = "myapp://www.test.com_uid=123";     var Android_URL = "myapp://www.test.com/openwith?uid=123";    var mtUrl = "http://www.test.com/download";    function open_link() {        window.location=mtUrl;    }    function try_to_open_app() {        setTimeout('open_link()', 500);    }    //IOS    if(browser.versions.iPhone){        document.getElementById("vlink").setAttribute("href",iOS_URL);        document.getElementById("vlink").click();    }    //Android    else if(browser.versions.android){             document.getElementById("vlink").setAttribute("href",Android_URL);        document.getElementById("vlink").click();    }         else{        open_link();    }</script></body></html>

2、配置入口

123456789101112131415161718192021222324252627 <!-- 默认入口 --><activity    android:name="net.laobanquan.im.splash.StartActivity"    android:launchMode="singleTop"    android:screenOrientation="portrait"    android:theme="@android:style/Theme.Black.NoTitleBar" >    <intent-filter>        <action android:name="android.intent.action.MAIN" />         <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity> <!-- 新建入口 --><activity    android:name="net.test.WebStartActivity"    android:screenOrientation="portrait">                 <intent-filter>        <action android:name="android.intent.action.VIEW"></action>        <category android:name="android.intent.category.DEFAULT"></category>        <category android:name="android.intent.category.BROWSABLE"></category>                 <data android:scheme="myapp" android:host="www.test.com" android:path="/openwith"/>    </intent-filter>     </activity>

3、Activity入口接受参数

123456789 String action = getIntent().getAction();String uid = null;if(Intent.ACTION_VIEW.equals(action)){      Uri uri = getIntent().getData();      if(uri != null){          uid = uri.getQueryParameter("uid");    }  }Log.d(TAG, uid);

4、总结

如果原来点击图片的默认入口是StartActivity,那么最好新建WebStartActivity入口,除了多做“接受参数”其他和StartActivity一样

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295