Nic*_*sen 33 android http intentfilter
我有一个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)
仍然没有去.我不确定重定向是否相关,浏览器显然首先进入非重定向网站,然后重定向到登录页面,但在任何时候我都无法选择在我的应用程序中打开它.此外,如果我首先在浏览器中手动登录,则没有重定向,但仍然无效.
我错过了一些明显的东西吗?我把我的头发拉出来为什么这不起作用,除了尝试我能想到的每一种可能的组合之外我都无法调试它(我做了......).谢谢你的帮助!
nur*_*eta 61
我想我会在这里发布这个,因为我花了一些时间来研究为什么我的意图过滤器不起作用.事实证明它一直在运作,但匹配是准确的.因此,如果您注册http://myexample.com的意图并且单击http://myexample.com/blah则无效.
添加以下内容解决了以下问题:
<data android:pathPattern="/.*" />
Run Code Online (Sandbox Code Playgroud)
所以intent过滤器看起来像:
<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>
Run Code Online (Sandbox Code Playgroud)
Anu*_*nuj 33
在你的使用中使用这三个 <intent filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
Run Code Online (Sandbox Code Playgroud)
小智 10
@nurieta,你的答案为我做了诀窍谢谢!
由于我正在处理潜在的查询字符串,因此我通过.java中的一些代码处理它,以确定是仅打开应用程序还是将您发送到应用程序中的特定位置.我的应用程序只是一个运行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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41162 次 |
| 最近记录: |