当在Android上点击一个URL时,我的应用会被打开。

5

我已经定义了一个意图过滤器,以便从某些类型的URL启动我的应用程序。问题是,它会为所有类型的链接启动,但我只想被一个具体的主机名启动。这是我的清单:

    <activity
        android:name="com.imin.SplashActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:logo="@drawable/img_action_icon"
        android:screenOrientation="portrait"
        android:theme="@style/AppTeme.Fullscreen" >
        <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" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="imintheapp.com"
                android:pathPrefix="/events/get/"
                android:scheme="http" />
        </intent-filter>
    </activity>

我需要仅在以下情况下打开我的应用程序:http://imintheapp.com/events/get/xxxxxxxx,其中xxxxxxxx是一个字母数字字符串。 请问我做错了什么吗? 谢谢

看起来没问题。你是说无论在哪个应用程序中的任何链接,不管域名如何,你的应用程序都是打开URL的选项之一?例如,在Chrome中,如果您单击指向www.google.com的链接,您的应用程序将启动或成为呈现给用户的选项之一? - Rich
是的!就是这样,任何网址都可以打开应用程序,例如www.google.com。 - Didac Perez Parera
我刚刚测试了一下,按预期工作,即只有形式为 http://imintheapp.com/events/get/xxxxxxxx 的 Intent URI 才会解析到您的活动。您使用的是哪个版本的 Android? - Reuben Scratton
3个回答

3
请在Menifest.xml文件中添加以下代码。
           <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="www.parag-chauhan.com"
                    android:path="/test"
                    android:scheme="http" />
            </intent-filter>

为了测试,请创建另一个项目并运行下面的代码。
Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse("http://www.parag-chauhan.com/test"));
startActivity(i);

您还可以在链接中传递参数,了解更多信息请点击此处


1
@DídacPérez,对我来说它运行良好。你是否使用Intent检查了调用URL? - Parag Chauhan
不在我的设备上。如果我选择“不再询问”,像www.google.com这样的链接也会启动我的应用程序……这毫无意义。 - Didac Perez Parera
嗨Parag。你能看一下这个问题吗?我尝试了相同的方法,但在5.1(棒棒糖)上没有任何反应。我想要从浏览器共享到我的应用程序的任何链接,并且我想要该帖子的标题。例如:Gmail。非常感谢你的时间和帮助http://stackoverflow.com/questions/32218607/get-your-app-when-share-is-clicked-on-chrome-url-in-android-in-5-1-lollipop。谢谢 - jason
@jason 你能详细解释一下吗? - Parag Chauhan
@ParagChauhan 感谢您的回复。我已将链接添加到我的清单中,只有当我点击任何链接时才会打开应用程序,但是当我尝试从Android中的Chrome浏览器共享我的链接时却无法打开。我不知道为什么会发生这种情况。我非常感谢您的帮助。添加了新的问题http://stackoverflow.com/questions/32239039/my-app-is-not-displayed-when-i-click-on-share-on-browser-in-lollipop-5-1 谢谢。 - jason
尽管我已经尝试过使用这些文件,但它仍然无法正常工作。 - Ahmad Arslan

1

试试这个

<data
            android:host="imintheapp.com"
            android:path="/events/get/"
            android:scheme="http" />

1
我认为你应该查看Manifest文件中的intent-filter元素。具体地,查看data子元素的文档。
请再次阅读这些文档。
我还建议您阅读this Que.的第二个答案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接