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

背景

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

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

xml 复制代码
<activity
    android: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() 中打印一个日志来表明它已经在那里了。

使用如下测试深层链接。

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

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

powershell 复制代码
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

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

powershell 复制代码
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中。

xml 复制代码
android:launchMode="singleTask"

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

java 复制代码
@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);
    }
}
相关推荐
摇滚侠7 分钟前
零基础小白自学 Git_Github 教程,GitHub Action 基础概念,笔记22
笔记·git·github
2501_9151063226 分钟前
如何查看手机使用记录:Android和iOS设备全面指南
android·ios·智能手机·小程序·uni-app·iphone·webview
爱丽_1 小时前
深入理解 Java Socket 编程与线程池:从阻塞 I/O 到高并发处理
java·开发语言
济南壹软网络科技有限公司1 小时前
云脉IM的高性能消息路由与离线推送机制摘要:消息的“零丢失、低延迟”之道
java·即时通讯源码·开源im·企业im
Seven971 小时前
剑指offer-46、孩⼦们的游戏(圆圈中最后剩下的数)
java
serendipity_hky1 小时前
互联网大厂Java面试故事:核心技术栈与场景化业务问题实战解析
java·spring boot·redis·elasticsearch·微服务·消息队列·内容社区
我真不会起名字啊1 小时前
C、C++中的sprintf和stringstream的使用
java·c语言·c++
十点摆码1 小时前
Spring Boot2 使用 Flyway 管理数据库版本
java·flyway·数据库脚本·springboo2·数据库脚本自动管理
linly12191 小时前
ERP学习笔记-频域分析之小波变换fieldtrip
笔记·学习
毕设源码-钟学长2 小时前
【开题答辩全过程】以 基于Javaweb的电动汽车充电桩管理系统为例,包含答辩的问题和答案
java·spring boot