网上对于安卓DeepLink方式跳转传递参数的例子较少,说的也不客观,实践之后发现还是有一些坑。其实为什么要用DeepLink方式跳转,有些是因为引流的原因,他们希望通过网页就能直接跳转到App的界面。还有其实就是某些业务的需要,需要统一跳转方式,方便维护代码。如果不知道DeepLink是什么,可以自行百度一下,下面介绍一下实际的用法:

接收参数方:

1.跳转的App需要在清单文件注册以下是例子:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alex.deeplinkproject"> <uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<!--url跳转格式为:open://app.test.com/game-->
<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="open"
android:host="app.test.com"
android:pathPrefix="/game"
/>
</intent-filter> </activity>
</application> </manifest>

通过三个字段生成一个URL:scheme://host pathPrefix 如上:open://app.test.com/game

2 需要接收的参数通过Uri获取

//通过Deeplink 跳转获取参数
String action = getIntent().getAction();
if (Intent.ACTION_VIEW.equals(action)) {
Uri data = getIntent().getData();
if (data != null) {
String appId = data.getQueryParameter("appId");
String token = data.getQueryParameter("token");
String extend = data.getQueryParameter("extend");
String merchant = data.getQueryParameter("merchant");
String agent = data.getQueryParameter("agent");
}

发送参数方(以下代码比较简单使用Kotlin编写):

1.需要传递对应的参数而后拼接到Uri后面,以下是例子

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.bt1).setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("open://app.test.com/game?appId=com.game.sid21&token=21token&extend=21extend&merchant=21merchant&agent=21agent"))
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
}

类似GET请求已Key=Value的形式传递。注意,Uri.parse 中的参数不可用+号进行拼接会出现无法获取参数的情况。跳转到其他App采用开启新的栈方式,避免误认为是一个App。

以上就是一个完整的跳转流程代码,但是实际上,当被跳转的App已经启动的时候我们有时候会取不到数据,但是跳转是正常的跳转了。这边要注意我们使用的flag,当被启动的App已经启动,他会在onNewIntent()返回我们的正确的Intent而不是getIntent()了。你需要重写此方法获取最新的Intent。最好抽取一个方法出来,在onCreate()和onNewIntent()中都获取Intent()。如下:

  @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过Deeplink 跳转获取参数
getIntentData(getIntent()); } @Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
getIntentData(intent);
} private void getIntentData(Intent intent){
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
Uri data = getIntent().getData(); if (data != null) {
String appId = data.getQueryParameter("appId");
String token = data.getQueryParameter("token");
String extend = data.getQueryParameter("extend");
String merchant = data.getQueryParameter("merchant");
String agent = data.getQueryParameter("agent");
}
} }

Android通过DeepLink方式跳转其他App传递参数的更多相关文章

  1. 通过注册的URL Scheme向目标APP传递参数

    通过注册的URL Scheme向目标APP传递参数 通过URL Scheme启动APP很简单就可以做到,但有时候我们想在启动APP的时候传递一些参数,这个时候我们就可以通过URL Scheme自定义U ...

  2. android 通过子线程跳转activity并传递内容

    android 子线程中不能够更新ui已经根深蒂固在我的脑海里,当时也就理所当然的觉得子线程中也是不能够进行界面的跳转的,可是在后来的学习中,发现居然是能够通过子线程来进行activity的跳转时,立 ...

  3. 跳转页面,传递参数——android

    android 跳转页面并传递对象(实体类)——项目中是集港收货类 网上资料:两种传递方法Serializable,parcelable 优劣比较:Serializable数据更持久化,网络传输或数据 ...

  4. Android activity之间的跳转和数据传递

    1.Activity之间的跳转 并且 传递数据 A Activity进行的操作 Intent intent = new Intent(context, B.class); intent.putExtr ...

  5. Android 安卓实现页面相互跳转并相互传递参数

    一.对于两个页面之间相互传值,跳转的时候我们使用 startActivityForResult(intent,0),而不是startActivity(intent) 这个方法 第一个页面中在触发跳转的 ...

  6. 微信小程序 页面跳转navigator与传递参数

    页面之间跳转使用 navigator标签,wx.navigateTo ,wx.redirectTo 1.URL就是跳转的页面路径.上面代码中就是navigator目录下的navigator页面,tit ...

  7. [转载]LinkButton跳转页面及传递参数

    在DataList中使用LinkButton按钮(LinkButtonDelete),该按钮用于链接跳转到删除页面.在模板中双击该按钮,跳转到.cs页面.问题是我们如何获得该条信息的ID,如果不知道I ...

  8. Angularjs 跳转页面并传递参数的方法总结

    http://www.zhihu.com/question/33565135 http://www.codeproject.com/Articles/1073780/ASP-NET-MVC-CRUD- ...

  9. iOS 跳转到 App Store 下载评分页面

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

随机推荐

  1. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  2. 学习笔记之Cloud computing

    Cloud computing - Wikipedia https://en.wikipedia.org/wiki/Cloud_computing

  3. 接口测试3-2csv格式

    csv文件数据 IntellJ IDEA打开终端:view-tool windows-terminal,可以在终端中查看文件路径 阿里 马云 京东 刘强东 京东 马化腾 #java //读取csv文件 ...

  4. ubuntu14.04 login loop issue

    无法进入图形界面的所有问题几乎都碰到了,可惜尝试所有办法,还是各种broken packages 等,无法重装 ubuntu-desktop 成功. 耽误了2天,果断决定重装系统算了..尽管很多软件, ...

  5. 远程复制文件scp使用

    1. install sudo apt-get install openssh-client openssh-server 2. login ssh remoteuser@remoteIP 3. co ...

  6. 协同过滤CF算法之入门

    数据规整 首先将评分数据从 ratings.dat 中读出到一个 DataFrame 里: >>> import pandas as pd In [2]: import pandas ...

  7. 第4章 文件和目录(5)_贯穿案例2:mini shell(1)

    6. 贯穿案例2:mini shell(1) [阶段性任务]实现cd.pwd和quit命令 //job.h #ifndef __JOB_H__ #define __JOB_H__ //接收命令行参数 ...

  8. HIbernate编程模型

    1.Hibernate: ORM框架,简化SQL开发,编程接口丰富,简化JDBC编程 2.有点: Lazy机制配合Fetch的HQL高级查询,提高开发效率 难点:理解Lazy与Fetch JOIN的原 ...

  9. android 系统架构简介

    Android系统采取的是分层的架构,根据官方文档提供的架构图,我们将android的系统架构分成5层,如图: 1.Application Framework (应用框架) application f ...

  10. 2018-2019-2 《网络对抗技术》Exp6 信息搜集与漏洞扫描 Week9 20165233

    Exp6 信息搜集与漏洞扫描 目录 一.基础问题 二.实验步骤 实验点一:各种搜索技巧的应用 实验点二:DNS IP注册信息的查询 实验点三:基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具 ...