我有一个Android应用程序,人们用它来替代网站.因此,当用户遇到网站的URL时,我想让他们在我的应用程序中而不是在浏览器中"打开URL".换句话说,我希望弹出窗口可以让我们在我的应用程序和浏览器(以及可能的其他应用程序)之间进行选择.
我从各种渠道了解到,我需要在我的应用中为活动添加一个意图过滤器,其中"数据"过滤器会过滤正确形式的网址.
有问题的网站是http://members.iracing.com,因此我添加了以下intent过滤器:
<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>
Run Code Online (Sandbox Code Playgroud)
我尝试了各种形式的这些数据过滤器,例如使用具有这两个属性的单个"数据"节点:
<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>
Run Code Online (Sandbox Code Playgroud)
它根本不起作用.我不知道还能告诉你什么.我在我的网站上托管了一个简单的HTML页面,其中有几个链接指向该网站上的各个页面(所有页面都以"http://members.iracing.com/..."开头),当我点击其中任何页面时,它们只是打开在浏览器中没有问我想要使用哪个应用程序.我在模拟器上以及在我的物理设备上安装应用程序后都尝试了它,没有任何作用.我在一个完全BLANK,新的Android项目中尝试了这个,只是为了看看它是否有用,什么都没有.
然后我意识到该网站需要身份验证,如果您没有登录,则会重定向到https://members.iracing.com/membersite/login.jsp上的登录页面,因此我尝试用"https"替换该方案.我甚至尝试将主机改为"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>
Run Code Online (Sandbox Code Playgroud)
仍然没有去.我不确定重定向是否相关,浏览器显然首先进入非重定向网站,然后重定向到登录页面,但在任何时候我都无法选择在我的应用程序中打开它.此外,如果我首先在浏览器中手动登录,则没有重定向,但仍然无效.
我错过了一些明显的东西吗?我把我的头发拉出来为什么这不起作用,除了尝试我能想到的每一种可能的组合之外我都无法调试它(我做了......).谢谢你的帮助!