简体   繁体   English

使用 Intent-filter 从 URL 打开 Android 应用程序不起作用

[英]Open Android app from URL using intent-filter not working

I have an Android app that people use as a replacement for a website.我有一个 Android 应用程序,人们用它来替代网站。 Hence, when users encounter an URL to the website, I want to give them the option to "open the URL" in my app instead of in the browser.因此,当用户遇到网站的 URL 时,我想让他们选择在我的应用程序中而不是在浏览器中“打开 URL”。 In other words I want the popup to appear that lets them choose between my app and the browser (and possibly other apps).换句话说,我希望出现弹出窗口,让他们在我的应用程序和浏览器(可能还有其他应用程序)之间进行选择。

I understand from various sources that I need to add an intent filter to an activity in my app with the 'data' filter that filters on URLs of the correct form.我从各种来源了解到,我需要使用“数据”过滤器向应用中的 Activity 添加一个意图过滤器,该过滤器可以过滤正确形式的 URL。

The website in question is http://members.iracing.com , hence I have added the following intent filter:有问题的网站是http://members.iracing.com ,因此我添加了以下意图过滤器:

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <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" />
            <data android:scheme="http" />
            <data android:host="members.iracing.com" />
        </intent-filter>
    </activity>

I have tried various forms of these data filters, like using a single 'data' node with both attributes:我尝试了这些数据过滤器的各种形式,例如使用具有两个属性的单个“数据”节点:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" android:host="members.iracing.com"/>
        </intent-filter>

It is simply not working.它根本不起作用。 I don't know what else to tell you.我不知道还能告诉你什么。 I hosted a simple HTML page on my website with a couple links to various pages on that website (all starting with "http://members.iracing.com/...") and when I click any of them, they simply open in the browser without ever asking me which app I want to use.我在我的网站上托管了一个简单的 HTML 页面,其中有几个链接到该网站上的各个页面(均以“http://members.iracing.com/...”开头),当我单击其中任何一个时,它们只需打开在浏览器中,而不会问我想使用哪个应用程序。 I tried it both on the emulator as well as after installing the app on my physical device, nothing works.我在模拟器上以及在我的物理设备上安装应用程序后都尝试过,但没有任何效果。 I tried this in a completely BLANK, new Android project just to see if that would work, nothing.我在一个完全空白的新 Android 项目中尝试了这个,只是为了看看它是否可行,没有。

I then realized that the website requires authentication, and if you are not logged in it redirects to the login page at https://members.iracing.com/membersite/login.jsp , hence I tried replacing the scheme by "https".然后我意识到该网站需要身份验证,如果您没有登录,它会重定向到https://members.iracing.com/membersite/login.jsp的登录页面,因此我尝试用“https”替换该方案。 I even tried changing the host to "www.members.iracing.com", and in the end I even tried a combination of all these things (not sure if this should work, but hey, I'm desperate at this point.....)我什至尝试将主机更改为“www.members.iracing.com”,最后我什至尝试了所有这些的组合(不确定这是否可行,但是,嘿,此时我很绝望.. ...)

       <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:host="members.iracing.com" />
            <data android:host="www.members.iracing.com" />
        </intent-filter>

Still no go.还是不行。 I'm not sure if the redirect is relevant though, the browser clearly first goes to the non-redirected site, then does the redirect to the login page, but at no point do I get the choice to open it in my app.我不确定重定向是否相关,但浏览器显然首先转到未重定向的站点,然后重定向到登录页面,但在任何时候我都无法选择在我的应用程序中打开它。 Furthermore, if I login manually in the browser first, there is no redirect, and it still does not work.此外,如果我先在浏览器中手动登录,则没有重定向,它仍然不起作用。

Am I missing something obvious here?我在这里遗漏了一些明显的东西吗? I'm pulling my hair out why this isn't working, and I cannot debug it besides trying every possible combination I could think of (I did...).我正在纠结为什么这不起作用,除了尝试我能想到的所有可能的组合(我做到了......)之外,我无法调试它。 Thanks for any help!谢谢你的帮助!

I thought I will post this here since I spent some time looking into why my intent filter did not work.我想我会在这里发布这个,因为我花了一些时间研究为什么我的意图过滤器不起作用。 It turns out that it was working all along but matches are exact.事实证明它一直在工作,但匹配是精确的。 Thus if you register your intent for http://myexample.com and you click in http://myexample.com/blah it will not work.因此,如果您为http://myexample.com注册您的意图并单击http://myexample.com/blah ,它将不起作用。

Adding the following fixed the issue:添加以下内容修复了问题:

<data android:pathPattern="/.*" />

So the intent filter looks like:所以意图过滤器看起来像:

<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:host="example.com" />
            <data android:scheme="https" />
            <data android:pathPattern="/.*" />
</intent-filter>

use these three in your <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" />

@nurieta, your answer did the trick for me thanks! @nurieta,您的回答对我有用,谢谢!

One word of advice since I am dealing with potential query strings, I handled it through a bit of code in the .java in order to determine if to just open the app or to send you to a specific location in the app.一个忠告,因为我正在处理潜在的查询字符串,我通过 .java 中的一些代码来处理它,以确定是打开应用程序还是将您发送到应用程序中的特定位置。 My app is just a WebView that runs a JQuery mobile app.我的应用程序只是一个运行 JQuery 移动应用程序的 WebView。

    if(getIntent().getData()!=null){//check if intent is not null
        Uri data = getIntent().getData();//set a variable for the Intent
        String scheme = data.getScheme();//get the scheme (http,https)
        String fullPath = data.getEncodedSchemeSpecificPart();//get the full path -scheme - fragments

        combine = scheme+"://"+fullPath; //combine to get a full URI
    }

    String url = null;//declare variable to hold final URL
    if(combine!=null){//if combine variable is not empty then navigate to that full path
        url = combine;
    }
    else{//else open main page
        url = "http://www.example.com";
    }
    webView.load(url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM