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

6次阅读

共计 2214 个字符,预计需要花费 6 分钟才能阅读完成。

关上浏览器,扫描某个二维码时须要启动特定的 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 的申请时,如果本机装置了这个利用,零碎就会弹出一个抉择,询问你想应用浏览器关上,还是应用该利用关上。

正文完
 0