在 URL 更改(自定义方案)上运行代码而不启动新活动

发布于 2024-12-28 12:52:19 字数 427 浏览 1 评论 0原文

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

我尝试过在意图过滤器中使用其他操作,但我尝试过的操作(RUN 和 ATTACH_DATA)会导致当 url 更改时,“网页不可用”页面显示在我的 WebView 中。此外,我在这里找到的所有意图过滤器/自定义方案问题都涉及如何使用具有自定义方案的 url 启动新活动 - 使得很难搜索相反的内容。

谢谢!

I have set up a custom URL scheme for my app using intent filters. When the URL changes in my WebView to my custom scheme (mockup://pagechange), the intent filter correctly filters the intent to my app, launching a new instance of my activity. However, what I actually want to happen is for the intent filter to run a piece of code inside the existing activity rather than launching a new activity. Is this possible, and if so, how do I set this up? If not, any other ideas on how to accomplish the task of running code when the URL in my WebView changes to anything beginning with 'mockup'?

I have tried using other actions in the intent filter, but the ones I've tried (RUN and ATTACH_DATA) result in the "Web page not available" page being displayed in my WebView when the url changes. Also, all of the intent filter / custom scheme questions I've found here deal with figuring out how to start a new activity with a url with a custom scheme - makes it hard to search for the opposite.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾其所爱 2025-01-04 12:52:19

首先,在清单中添加 android:launchMode="singleTop" 您的活动声明。在您的意图中使用标志FLAG_ACTIVITY_CLEAR_TOP。您将在 onNewIntent() 回调中获得意图。

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

First, add android:launchMode="singleTop" your activity declaration in the manifest. In your intent use the flags FLAG_ACTIVITY_CLEAR_TOP. You'll get the intent in the onNewIntent() callback.

Remember that the intent delivered in onNewIntent() is not the same as returned in getIntent(). If you make use of getIntent() in other parts of your code, use setIntent on the intent from onNewIntent().

等往事风中吹 2025-01-04 12:52:19

我最终放弃了意图过滤器模式 - 我已经为我的 WebView 设置了一个 WebViewClient 来处理在我的 WebView 中加载外部链接,所以我只是在那里设置一个标志来运行我的代码,如果正在加载的 URL 包含我正在寻找的自定义方案:

WebView wv = (WebView) findViewById(R.id.app_container);
// [other setup]
wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            if(!url.contains("mockup://"))
                view.loadUrl(url);
            else {
                // run "mockup"-specific code
            }
            return true;
        }
    });

I ended up moving away from the intent filter pattern - I had already set up a WebViewClient for my WebView to handle loading external links in my WebView, so I just set a flag in there to run my code if the URL that is being loaded contains the custom scheme I'm looking for:

WebView wv = (WebView) findViewById(R.id.app_container);
// [other setup]
wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            if(!url.contains("mockup://"))
                view.loadUrl(url);
            else {
                // run "mockup"-specific code
            }
            return true;
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文