【问题标题】:Intent Filter to Launch My Activity when custom URI is clicked单击自定义 URI 时启动我的活动的意图过滤器
【发布时间】:2011-08-01 13:28:59
【问题描述】:

我正在尝试允许注册 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:我已经看过了 How to implement my very own URI scheme on AndroidHow to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in 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>

【问题讨论】:

    标签: android uri android-manifest android-intent


    【解决方案1】:

    最终的解决方案是涵盖所有基础的 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>
    

    【讨论】:

    • 嗨 tuxGurl,但这仍然不是一个合适的解决方案,因为您必须创建两个链接,对吧?一个用于 iphone 和 BB,另一个用于 Android。我们遇到了同样的问题,在将头撞到墙上后,恐怕我们必须在一些电子邮件中发送两个链接。
    【解决方案2】:

    当我使用 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 类别并将数据更改为&lt;data android:scheme="myapp" android:host="myapp.mycompany.com"/&gt;,但它没有帮助。电子邮件应用程序将 myapp.mycompany.com/index/customerId/12345 识别为浏览器链接,并且不会在我的应用程序中打开它。
    • 所以链接不包含 URL 的myapp:// 部分?您是否尝试过在浏览器中手动输入 URL 以查看是否有效?
    • 在电子邮件中,链接如下所示:myapp://myapp.mycompany.com。如果我从浏览器尝试,它会重定向到谷歌搜索。
    • @tuxGurl 你是怎么解决的?
    【解决方案3】:

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

    【讨论】:

    • 你验证过这个作品吗? 1 - biturl 不接受自定义模式 2 - tinyurl 接受,但其重定向不会打开应用程序。 3 - 如果我在浏览器栏 (chrome) 中输入自定义 url,应用程序不会加载,因为这不是作为全局意图发送的
    • 我成功地使用了这种方法。它不仅解决了 Gmail,还解决了链接的 NFC 和 QR 码传输,以便正确解释。
    • 我们用同样的方法和一个 servlet 来做重定向。像魅力一样工作。
    【解决方案4】:

    这对我来说是解决方案。谢谢@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>
    

    【讨论】:

      【解决方案5】:

      您是否已将类别添加到您的意图过滤器:

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

      【讨论】:

      • 是的,但这并没有什么不同。 :(
      【解决方案6】:

      您总是可以尝试使用 HTML 发送电子邮件,然后使用 &lt;a&gt; 标记来创建 URL。我认为没有办法改变 Gmail 或 Mail 解析其文本的方式,因为它们可能使用 Linkify 类。

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

      【讨论】:

      • 我认为使用 android:scheme 的目的是允许将 URI 注册到像 Blackberry 上的 PatternRepository 和 iPhone 上的 CFBundleURLNameCFBundleURLSchemes 这样的应用程序。如何在 Android 上获得相同的结果?
      • android:scheme 允许您使用自定义方案启动应用程序。但它不会自动解析其他应用程序中的纯文本。每当使用您的自定义方案单击链接时,您的应用程序都会启动。因此,您可以发送一封带有链接的电子邮件启动我的应用程序,这将启动您的应用程序。
      • 我尝试将链接包装在一个 href 中。在 Atrix 上,该链接带有下划线,但点击它没有任何作用。在 Galaxy 上,myapp.mycompany.com/index/customerId/12345 部分带有下划线并在浏览器中打开。我已经在主帖中更新了我的清单以代表我现在使用的内容。
      • 你试过&lt;data android:scheme="myapp" android:host="*"/&gt; 吗?
      【解决方案7】:

      我也遇到了这个问题,但是对于标准的 http: 方案 url。 Gmail 似乎没有向 Intent 添加任何类别。现在我检查 BROWSABLE,但我还检查了 !intent.hasCategories() 并允许它通过。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        相关资源
        最近更新 更多