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";
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
<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、配置入口

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
<!-- 默认入口 -->
<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入口接受参数

1
2
3
4
5
6
7
8
9
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一样

最新文章

  1. Atitit 为什么网络会有延时 电路交换与分组交换的区别
  2. Java TCP 程序
  3. 升级xcode7的问题:使用shareSDK,坑的你两眼泪汪汪
  4. Bash For Loop Examples for Your Linux Shell Scripting--ref
  5. CSS之使用display:inline-block来布局
  6. 五、Vi和Vim编辑器
  7. 2018总结--&gt;2019新目标
  8. c# 7.1 Async Main方法
  9. 蘑菇街支付架构 PDF 下载
  10. ManageEngine SeviceDesk Plus帮助台和资产管理软件
  11. KEIL中函数定义存在但go to definition却不跳转的原因
  12. javascript数据结构与算法--高级排序算法(快速排序法,希尔排序法)
  13. 第一篇:初识ASP.NET控件开发_第一节:控件类及其继承关系
  14. Auty 2017——WebMonitor接口线上检测平台
  15. iscroll5实现下拉加载更多
  16. Unity3D笔记 愤怒的小鸟&lt;四&gt; 实现Selelction界面
  17. 5Lambda表达式
  18. mrh支付宝玩转福
  19. CSS border-right-style属性设置元素的右边框样式
  20. 使用 Php Artisan Tinker 来调试你的 Laravel

热门文章

  1. USB相关的网络资料
  2. ZOJ 1240 IBM Minus One
  3. JQuery onload、ready概念介绍及使用方法
  4. HDU 1231 最大连续子序列 --- 入门DP
  5. 课堂所讲整理:HTML--8Window.document对象
  6. 如何在python3.3用 map filter reduce
  7. 让边框和文本一样高,不受line-height影响,可以使用padding,padding可以用于行内元素
  8. spring注解方式在一个普通的java类里面注入dao
  9. 20150906VS小知识
  10. 让EDIUS中素材小距离精确移动的教程