当前位置: 首页 > news >正文

Android笔记(三十一):Deeplink失效问题

背景

通过deeplink启动应用之后,没关闭应用的情况下,再次使用deeplink会失效的问题,是系统bug导致的。此bug仅在某些设备(Nexus 5X)上重现,launchMode并且仅当应用程序最初通过深层链接启动并再次通过深层链接打开时才会重现。
在这里插入图片描述

在AndroidManifest中像这样设置了我的活动,即MainActivity。

<activityandroid:name="com.package.name.MainActivity"android:screenOrientation="portrait"android:theme="@style/Theme.AppTheme"><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="myscheme" android:host="myhost" /></intent-filter>
</activity>

并且在 LaunchActivity.onCreate() 中打印一个日志来表明它已经在那里了。
使用如下测试深层链接。

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name

在应用程序被终止后,使用上述命令,它可以打开应用程序并路由到正确的活动,没有问题。并且有以下日志。

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name
Starting: Intent { act=android.intent.action.VIEW dat=dlscheme://dlhost/param pkg=some.package.name }
Status: ok
Activity: some.package.name/.activity.LaunchActivity
ThisTime: 898
TotalTime: 898
WaitTime: 919
Complete

但是,如果再次输入相同的命令,而不终止应用程序。它只会打开应用程序,但不会打开正确的活动,并生成以下日志。

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name
Starting: Intent { act=android.intent.action.VIEW dat=dlscheme://dlhost/param pkg=some.package.name }
Warning: Activity not started, its current task has been brought to the front
Status: ok
Activity: some.package.name/.activity.LaunchActivity
ThisTime: 0
TotalTime: 0
WaitTime: 6
Complete

注意到有这一行
Warning: Activity not started, its current task has been brought to the front

解决方案

在项目的清单文件中,需要将以下内容添加到MainActivity中。

android:launchMode="singleTask"

并处理内部的深层链接onNewIntent()

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_recipe);onNewIntent(getIntent());
}protected void onNewIntent(Intent intent) {String action = intent.getAction();String data = intent.getDataString();if (Intent.ACTION_VIEW.equals(action) && data != null) {String recipeId = data.substring(data.lastIndexOf("/") + 1);Uri contentUri = RecipeContentProvider.CONTENT_URI.buildUpon().appendPath(recipeId).build();showRecipe(contentUri);}
}
http://www.lryc.cn/news/478988.html

相关文章:

  • 图神经网络初步实验
  • 创建线程时传递参数给线程
  • 兴业严选|美国总统都是不良资产出身 法拍市场是否将大众化
  • C#-拓展方法
  • 加锁失效,非锁之过,加之错也|京东零售供应链库存研发实践
  • vue3 传值的几种方式
  • AUTOSAR CP NVRAM Manager规范导读
  • 2024阿里云CTF Web writeup
  • 软件著作权申请教程(超详细)(2024新版)软著申请
  • 三维测量与建模笔记 - 3.2 直接线性变换法标定DLT
  • Whisper AI视频(音频)转文本
  • 全网最详细RabbitMQ教学包括如何安装环境【RabbitMQ】RabbitMQ + Spring Boot集成零基础入门(基础篇)
  • esp32记录一次错误
  • Moonshine - 新型开源ASR(语音识别)模型,体积小,速度快,比OpenAI Whisper快五倍 本地一键整合包下载
  • java-web-苍穹外卖-day1:软件开发步骤简化版+后端环境搭建
  • 一个国产 API 开源项目,在 ProductHunt 杀疯了...
  • 斗破QT编程入门系列之二:认识Qt:编写一个HelloWorld程序(四星斗师)
  • 木马病毒相关知识
  • 用 Python 写了一个天天酷跑(附源码)
  • 【网络-交换机】生成树协议、环路检测
  • C++ 中的 JSON 序列化和反序列化:结构体与枚举类型的处理
  • MySQL 批量删除海量数据的几种方法
  • 【docker入门】docker的安装
  • 单例模式五种写法
  • 解析静态链接
  • 前端基础-html-注册界面
  • 量子电路的实现 基于ibm的qiskit
  • 关于谷歌浏览器debug模式不进断点问题解决方案
  • 制造行业实践|悠进电装基于超融合完成信息化改造, 保障业务系统 7/24 长跑
  • 如何学习C++游戏开发