关于android:浏览器扫码打开AndroidiOS-App

关上浏览器,扫描某个二维码时须要启动特定的App,要实现这样的需要,咱们首先须要解析HTML页面的二维码,通常解析后的内容格局为:

<a href="[scheme]://[host]/[path]?[query]">启动App</a> 
//测试链接
<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动App</a> 
  • scheme:判断启动的App。
  • host:适当记述。
  • path:传值时必须的key,没有能够不传。
  • query:获取值的Key和Value,没有能够不传。

首先,咱们关上Android工程的AndroidManifest.xml配置文件,而后在启动页面Activity下追加以下内容。

<intent-filter>  
    <action android:name="android.intent.action.VIEW"/>  
    <category android:name="android.intent.category.DEFAULT" />  
    <category android:name="android.intent.category.BROWSABLE" />  
    <data android:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>

须要阐明的是,intent-filter的内容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】这2个,不能与这次追加的内容混合在一起。因为如果退出了同一个Activity,会导致利用图标在桌面隐没等问题,所以个别状况配置如下。

<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.DEFAULT" />  
    <category android:name="android.intent.category.BROWSABLE" />  
    <data android:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter> 

接下来,在Activity中须要取值的中央增加以下代码,能够间接写在OnCreate函数里的。

val value = intent
        val action = value.action
        if (Intent.ACTION_VIEW == action) {
            val uri: Uri? = value.data
            if (uri != null) {
                val name: String? = uri.getQueryParameter("name")
               val age: String? = uri.getQueryParameter("age")
                ...  //解决业务
            }
        }

通过下面的解决后,就能够实现浏览器扫码关上Android/iOS App的性能了。须要留神的是,肯定要用自带浏览器或者谷歌浏览器,不要用什么Uc、腾讯浏览器。

最近遇到这么一个需要: 当用户在手机浏览器中点击一个按钮时,如果手机上曾经该应用程序,则间接关上,如果没有装置,则转向利用下载页面。 对于这一需要,能够应用【scheme://host:port/path or pathPrefix or pathPattern】的形式,上面的Android的开发文档:

http://developer.android.com/guide/topics/manifest/data-element.html

上面是具体的示例代码:

<a id="applink1" href="http://test.xx.com/demo/test.php">关上</a>

而后,给指标Activity减少以下filter。

<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
                     
    <data
       android:host="test.xx.com "
       android:path="/demo/test.php"
       android:scheme="http" /> 
</intent-filter>

减少该filter后,该Activity就能解决 http://test.xx.com/demo/test.php。在浏览器中点击“开始”,发动对该URL的申请时,如果本机装置了这个利用,零碎就会弹出一个抉择,询问你想应用浏览器关上,还是应用该利用关上。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理