当自定义URI被点击时,使用意图过滤器启动我的活动

24

我正在尝试允许一个URI链接可以通过我的应用程序打开,类似于黑莓上的PatternRepository和iPhone上的CFBundleURLName/CFBundleURLSchemes。如何在Android上实现相同的结果?

系统将发送一些包含以下链接的电子邮件:myapp://myapp.mycompany.com/index/customerId/12345。想法是用户应该能够点击该链接以在应用程序中打开客户活动。

我尝试了其他SO帖子中的许多建议,但无法使操作系统识别模式并打开我的应用程序。

在Gmail应用程序中,它看起来像这样:myapp://myapp.mycompany.com/index/customerId/12345。它识别并下划线显示链接的myapp.mycompany.com/index/customerId/12345部分,并在浏览器中打开它。 myapp:// 部分没有被链接化。

标准邮件应用程序将整个链接视为纯文本。

我在这里缺少什么?

PS: 我已经查看了如何在Android上实现自己的URI方案如何注册一些URL命名空间(myapp://app.start/)以通过在浏览器中调用URL访问您的程序在Android OS中?

清单文件:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="2"
    android:versionName="0.0.8" 
    package="com.mycompany.myapp.client.android">

    <uses-sdk 
        android:minSdkVersion="7" 
        android:targetSdkVersion="7"/>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application 
        android:label="@string/app_name" 
        android:name="myappApplication" 
        android:icon="@drawable/ic_icon_myapp" 
        android:debuggable="true">

        <activity 
            android:label="My App" 
            android:name=".gui.activity.LoginActivity" 
            label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity android:name=".gui.activity.CustomerDetailActivity" > 
            <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:scheme="myapp"/> 
            </intent-filter> 
        </activity>

        <activity android:name=".gui.activity.CustomerDetailActivity"/>
        <activity android:name=".gui.activity.CustomerImageViewerActivity" />
        <activity android:name=".gui.activity.CustomerListActivity" android:configChanges="orientation|keyboardHidden"/>
        <activity android:name=".gui.activity.HomeActivity" android:configChanges="orientation|keyboardHidden"/>
        <activity android:name=".gui.activity.AboutActivity" android:configChanges="orientation|keyboardHidden"/>
        <activity android:name=".gui.activity.AccountActivity" android:configChanges="orientation|keyboardHidden" />  
    </application>
</manifest>
7个回答

13
最终的解决方案是一个hacky的变通方法,以涵盖所有情况。现在电子邮件还包含一个扩展名已经注册为与该应用程序一起打开的附件。
AndroidManifest.xml:
    <activity android:name=".gui.activity.CustomerDetailActivity" > 
        <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:scheme="https"
                 android:host="myapp.mycompany.com" /> 
        </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:scheme="myapp"
                 android:host="myapp.mycompany.com" /> 
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.EDIT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:mimeType="application/myapp" />
        </intent-filter>
    </activity>

1
嗨tuxGurl,但是,这不是一个正确的解决方案,因为您必须创建两个链接,对吧?一个用于iPhone和BB,另一个用于Android。我们遇到了同样的问题,在撞墙后,恐怕我们将不得不在某些电子邮件中发送两个链接。 - Maragues

6

当我在使用Google日历进行OAuth时,我需要向我想要接收回调的活动添加此过滤器:

<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:scheme="yourapp" android:host="goog"></data>
</intent-filter>

当浏览器调用yourapp://goog URL时,它会返回到我的Activity。

我添加了“BROWSABLE”类别,并将数据更改为<data android:scheme="myapp" android:host="myapp.mycompany.com"/>,但这并没有起到帮助作用。邮件应用程序将myapp.mycompany.com/index/customerId/12345识别为浏览器链接,并未在我的应用程序中打开它。 - tuxGurl
1
所以链接中不包含URL的 myapp:// 部分?您是否尝试手动在浏览器中输入URL来查看它是否有效? - BigFwoosh
10
在电子邮件中,链接的格式看起来是这样的:myapp://myapp.mycompany.com。如果我在浏览器中尝试打开它,会被重定向到 Google 搜索页面。 - tuxGurl
@tuxGurl 你是怎么解决的? - Sazzad Hissain Khan

4
您可以通过使用带有302重定向的标准HTTP URL来解决GMail不链接非标准协议的问题。您可以在您的网站Web服务器或应用程序服务器上设置它,或者为了快速测试,您可以使用URL缩短服务,如http://bit.ly

2
你验证过这个是否有效吗?1 - biturl不接受自定义模式,2 - tinyurl可以,但其重定向功能无法打开应用程序。3 - 如果我在浏览器栏(Chrome)中输入自定义URL,则该应用程序不会加载,因为它不被发送为全局意图。 - Aiden Fry
我使用这种方法取得了成功。它不仅解决了Gmail的问题,还解决了NFC和QR码传输链接的问题,以便正确解释。 - Andrew Arnott
我们使用同样的方法,通过一个servlet进行重定向。效果非常好。 - Mark Freeman

1
这是关于编程的解决方案。感谢 @DanO。
<intent-filter>
        <data android:scheme="yourcustomname"/>
        <data android:host="*"/>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
  </intent-filter>

0

你尝试过在你的intent-filter中添加一个类别吗:

<category android:name="android.intent.category.BROWSABLE" />

是的,但这并没有什么区别。:( - tuxGurl

0

你可以尝试使用HTML发送电子邮件,然后使用<a>标签创建URL。我认为没有办法改变Gmail或Mail解析文本的方式,因为它们可能使用Linkify类。

另一个选择是使用http://,然后只解析特定自定义子域,这将为您的用户提供在浏览器或应用程序中打开的选项。


我认为使用android:scheme的目的是允许将URI注册到应用程序中,就像在Blackberry上的PatternRepository和iPhone上的CFBundleURLName和CFBundleURLSchemes一样。那么我该如何在Android上实现相同的结果呢? - tuxGurl
android:scheme 可以让你使用自定义方案启动应用程序。但它不会自动解析其他应用程序中的纯文本。每当使用你的自定义方案点击链接时,你的应用程序都将启动。因此,你可以发送一个带有链接<a href="myapp://myapp.mycompany.com/index/customerId/12345">启动我的应用</a>的电子邮件,这将启动你的应用程序。 - DanO
我尝试将链接包装在href中。在Atrix上,链接有下划线,但点击它没有任何反应。在Galaxy上,myapp.mycompany.com/index/customerId/12345部分有下划线,并在浏览器中打开。我已经更新了我的清单,在主帖中表示我现在正在使用的内容。 - tuxGurl
你尝试过使用 <data android:scheme="myapp" android:host="*"/> 吗? - DanO

0

我也遇到了这个问题,但是针对标准的http:协议URL。 Gmail似乎没有向Intent添加任何类别。 现在我检查BROWSABLE,但我还包括一个检查!intent.hasCategories()并允许其通过。


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