Android通过DeepLink方式跳转其他App传递参数
网上对于安卓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传递参数的更多相关文章
- 通过注册的URL Scheme向目标APP传递参数
通过注册的URL Scheme向目标APP传递参数 通过URL Scheme启动APP很简单就可以做到,但有时候我们想在启动APP的时候传递一些参数,这个时候我们就可以通过URL Scheme自定义U ...
- android 通过子线程跳转activity并传递内容
android 子线程中不能够更新ui已经根深蒂固在我的脑海里,当时也就理所当然的觉得子线程中也是不能够进行界面的跳转的,可是在后来的学习中,发现居然是能够通过子线程来进行activity的跳转时,立 ...
- 跳转页面,传递参数——android
android 跳转页面并传递对象(实体类)——项目中是集港收货类 网上资料:两种传递方法Serializable,parcelable 优劣比较:Serializable数据更持久化,网络传输或数据 ...
- Android activity之间的跳转和数据传递
1.Activity之间的跳转 并且 传递数据 A Activity进行的操作 Intent intent = new Intent(context, B.class); intent.putExtr ...
- Android 安卓实现页面相互跳转并相互传递参数
一.对于两个页面之间相互传值,跳转的时候我们使用 startActivityForResult(intent,0),而不是startActivity(intent) 这个方法 第一个页面中在触发跳转的 ...
- 微信小程序 页面跳转navigator与传递参数
页面之间跳转使用 navigator标签,wx.navigateTo ,wx.redirectTo 1.URL就是跳转的页面路径.上面代码中就是navigator目录下的navigator页面,tit ...
- [转载]LinkButton跳转页面及传递参数
在DataList中使用LinkButton按钮(LinkButtonDelete),该按钮用于链接跳转到删除页面.在模板中双击该按钮,跳转到.cs页面.问题是我们如何获得该条信息的ID,如果不知道I ...
- Angularjs 跳转页面并传递参数的方法总结
http://www.zhihu.com/question/33565135 http://www.codeproject.com/Articles/1073780/ASP-NET-MVC-CRUD- ...
- iOS 跳转到 App Store 下载评分页面
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
随机推荐
- make_heap()等函数的用法
1.make_heap() make_heap()用于把一个可迭代容器变成一个堆,默认是大顶堆. 它有三个参数.第一个参数是指向开始元素的迭代器,第二个参数是指向最末尾元素的迭代器,第三个参数是les ...
- loadrunner怎么解决录制完成后脚本为空
第一步: 第二步: 设置完后就Ok了
- python 可视化 二维坐标标注等等
基本画图操作: import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = ...
- [转]C# 安装布署
C# 安装布署 及Windows服务自动启动 分类: asp.net2009-09-23 10:43 1126人阅读 评论(0) 收藏 举报 windowsc#serviceobject服务器 设置s ...
- 在Linux 系统 Latex安装 使用入门教程
来源: http://blog.chinaunix.net/u/25605/showart_2100398.html 入门介绍好文:TeX.LaTeX.TeXLive 小结 笔记详情:http://v ...
- Java笔试基础01
单例模式主要作用是保证在Java应用程序内,一个类只有一个实例存在. 手写单例 1.较为安全的写法 public class Singleton01{ private static Singleton ...
- uva-10004-俩色图验证
题意: 在1976年,四色猜想被一个计算机助手提出,这个理论表示对任意一个地图的上色都只需要四种颜色,地图内每一个区块和相邻的区块颜色都不相同.你现在被要求解决一个相似但相对简单的问题.给你任意一个连 ...
- 表单:提交验证,及blur事件验证
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Eclipse安装STS(Spring Tool Suite (STS) for Eclipse)插件
转自:https://blog.csdn.net/zhen_6137/article/details/79383941
- delphi const的用法
unit RadKeygen; interface uses Classes,SysUtils,Windows; function fun1():string; implementation cons ...