2010-03-05 95 views
4

我试图在用户浏览到某个网址时启动我的应用程序。我发现了一些例子,它们在清单中都有相同的东西,但它不适合我。我已经把意图过滤器放在Activity和Receiver之下。从URL启动活动

这里是我的清单片段:

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

当活动下,我尝试使用onNewIntent,当它使用的是接收器,我使用onReceiveIntent尝试,都用一个简单的Log.i电话,看是否它是否被解雇。我没有太多的运气。

+0

相关问题:http://stackoverflow.com/questions/2448213/how-to-implement-my-very-own-uri-schema-on-android – Casebash 2010-05-26 05:57:36

回答

7

我在manifest.xml文件使用:

<activity android:name=".SomeName"> 
    <intent-filter> 
     <category android:name="android.intent.category.ALTERNATIVE" /> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 
     <data android:host="google.com" android:scheme="http" /> 
    </intent-filter> 
</activity> 

这将启动活动SomeName。我不会在android:host部分使用www,也许会有所作为。

当活动开始,你可以得到这就是使用.COM背后的数据(例如):

Uri data = getIntent().getData(); 
if(data != null && data.getPathSegments().size() >= 2){ 
    List<String> params = data.getPathSegments(); 
    String somestuff = params.get(0); 
} 

编辑:如果你wan't能够从活动中检查主机,使用此方法:

data.getHost(); 
+0

与Android这项工作棒糖? – 2015-04-14 06:47:07

+0

是的,它确实工作;) – 2015-04-14 06:50:20

+0

你能指导我们在这里http://stackoverflow.com/q/29116574/1071545 – 2015-04-14 07:38:32