Android development – Open or download Android apps by scanning QR code

Source: Internet
Author: User

In the realization of this function, by the different browsers tortured stomach pain, finally achieved a barely able to use, but also a look at other people's blog

Android allows you to open a local app (app) and get the data from your browser by clicking the link in the browser

Android/iphone: How to open an application directly from browser or open the store (if there is no application)

1, HTML page (JS is not the line, this is written by other people)

Need to pay attention to is android_url, the format needs to conform to [Scheme]://[host]/[path]? [Query]

Scheme: Identify the app that starts.

Host: Proper description

Path: Key to pass value (no or YES)

Query: Gets the value of the key and value (no also can)

1 Android_URL = "myapp://www.test.com/openwith?uid=123";

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 <html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"/><meta content="telephone=no"name="format-detection"/><metaname="viewport"content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />    <title>打开或下载应用</title>    <scriptsrc="assets/plugins/jquery-1.8.3.min.js"type="text/javascript"></script></head><body><aid="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. Configure the Entrance

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

3. The activity entrance accepts the parameter

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. Summary

If the original click on the image of the default entrance is StartActivity, then it is better to create a new webstartactivity entrance, in addition to do "Accept parameters" and other startactivity like

Android development – Open or download Android apps by scanning QR code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.