DeepLink(浏览器点击链接跳转到app)

来源:互联网 发布:日语网络用语 ggk 编辑:程序博客网 时间:2024/05/24 23:12

在AndroidManifest.xml中设置

添加权限

<uses-permission android:name="android.permission.INTERNET"/>

在Activity中添加intent-filter

            <intent-filter>                <action android:name="android.intent.action.VIEW"/>                <category android:name="android.intent.category.BROWSABLE"/>                <category android:name="android.intent.category.DEFAULT"/>                <data android:scheme="test" android:host="tp" android:pathPrefix="/open"/>            </intent-filter>

scheme,host,pathPrefix可以根据自己需要设置。
浏览器要跳转到此Activity,需要url格式为:test://tp/open
可以在后面添加额外参数,比如:test://tp/open?name=lucy&age=8
Activity不一定是首先打开的Activity。(比如有ActivityB界面,可以在ActivityB中添加上面的intent-filter)

完整AndroidManifest.xml格式如下:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.test.git.webviewdemo">    <uses-permission android:name="android.permission.INTERNET"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <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.BROWSABLE"/>                <category android:name="android.intent.category.DEFAULT"/>                <data android:scheme="test" android:host="tp" android:pathPrefix="/open"/>            </intent-filter>        </activity>    </application></manifest>

网页代码

<html><head>    <meta name="meta_name" content="helloworld"></head><body><a href="test://tp?name=miss&age=8">启动应用程序</a>//点击无法跳转<a href="test://tp/open?name=miss&age=8">启动应用程序</a>//点击可以跳转<a href="test://tp.app/open?name=miss&age=8">启动应用程序</a>//点击无法跳转</body></html>

要确保url的链接与test://tp/open匹配才行。
此外不能在浏览器中输入,只能是通过网页点击有效。

Activity的对应操作

        Intent intent = getIntent();        String action = intent.getAction();        if(Intent.ACTION_VIEW.equals(action)){            Uri uri = intent.getData();            if(uri != null){                String name = uri.getQueryParameter("name");                String age= uri.getQueryParameter("age");                Log.i("view_data", "name:" + name + " age:" + age);            }        }

在Activity的onCreate方法中,通过上面代码进行数据的解析,及进一步操作。

一些补充

可以只写scheme

<data android:scheme="test"/>

外部链接是test://格式的都会打开相应app。

对于传递参数

参数key=value格式,可以考虑把value进行encode。
app可以获取以后,进行一下decode。

跳转app时如果没有设置背景可能会黑屏或者白屏一段时间。

解决办法是:可以给跳转的app设置一个背景色。参考:应用启动白屏

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 C语言实验题――分数序列 C语言实验题――数组逆序 C语言实验题――一元二次方程 C语言实验题――某年某月的天数 相邻最大数 Picture 羊村运动会――撑杆跳高 字符矩阵排序 1535:找规律填数字 idm6.38 C语言实验题――圆周率 缺陷检测 成都瓶儿 谭松韵 桥本 YouTube、Google+、Facebook、Twitter、维基百科最全hosts C语言实验题――拍皮球 求奇数的乘积 C语言实验题――字符编码 intellijlicense andriod导航栏 mfc文件上传下载 Eclipse4.8中搭建Android开发环境 1178:C语言实验题――单词统计 现在小明决定实现一个类似malloc的内存分配系统,具体来说,他需要连续处理若干申请内存的请求,这个 给定某二叉树的前序序列和中序序列,输出该二叉树的后序序列。(输入的前序遍历和中序遍历的结果中都不含重 vibe twitter 幼呦 fasttunnel 一叫三吱 浩君恋母 C语言实现朴素贝叶斯 shadowsocks服务 CCF201509-2日期计算 song:平面上若干个点,求任意两点间的最长距离 求n阶乘(1<=n<=10000) 【问题描述】计算一个班级N个学生三门课的各门课平均分,三门课总分,并输出成绩最高者的信息【输入形式】 NOD32分享区 舞蹈生 busfan