android - 在不启动新 Activity 的情况下在 URL 更改(自定义方案)上运行代码

标签 android android-intent android-webview

我已经使用 Intent 过滤器为我的应用设置了自定义 URL 架构。当我的 WebView 中的 URL 更改为我的自定义方案 (mockup://pagechange) 时, Intent 过滤器正确地过滤了我的应用程序的 Intent ,启动了我的 Activity 的新实例。然而,我真正想要的是让 Intent 过滤器在现有 Activity 中运行一段代码,而不是启动一个新 Activity 。这可能吗?如果可以,我该如何设置?如果没有,关于当我的 WebView 中的 URL 更改为以“模型”开头的任何内容时如何完成运行代码的任务的任何其他想法?

我曾尝试在 intent 过滤器中使用其他操作,但我尝试过的操作(RUN 和 ATTACH_DATA)会导致“网页不可用”页面在 url 更改时显示在我的 WebView 中。此外,我在这里发现的所有 Intent 过滤器/自定义方案问题都涉及弄清楚如何使用带有自定义方案的 url 启动新 Activity - 很难搜索相反的内容。

谢谢!

最佳答案

首先,在 list 中添加 android:launchMode="singleTop" 您的 Activity 声明。在您的 Intent 中使用标志 FLAG_ACTIVITY_CLEAR_TOP。您将在 onNewIntent() 回调中获得 Intent 。

请记住,在 onNewIntent() 中传递的 Intent 与在 getIntent() 中返回的 Intent 不同。如果您在代码的其他部分使用 getIntent(),请对 onNewIntent() 的 Intent 使用 setIntent。

关于android - 在不启动新 Activity 的情况下在 URL 更改(自定义方案)上运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8966517/

相关文章:

cordova - 如何为 android 创建 cordova .jar 文件?

android - WebViewClient 不调用 shouldOverrideUrlLoading

Android adb 设备未经授权

android - travis 中存储库的路径

java - android.database.sqlite.SQLiteException : no such column: bucket_display_name

android - 在所有浏览器上添加自定义 header

Java 和安卓 : How to open several files with an Intent?

android - launchMode ="singleTask"不创建新任务

android - 在 OSMdroid 中找出当前位置

android - 如何检测android中webview是否存在缓存?