Intent Filter在单击自定义URI时启动My Activity

时间:2011-04-08 14:05:07

标签: android uri android-manifest android-intent

我正在尝试允许注册URI以使用我的应用打开。就像Blackberry上的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?

The Manifest:     

<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 个答案:

答案 0 :(得分:13)

最终解决方案是一个涵盖所有基础的hacky解决方法。该电子邮件现在还包含一个附件,其附加注册为随应用程序打开。

The Manifest:

        <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 :(得分: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网址时,它会返回我的活动。

答案 2 :(得分:4)

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

答案 3 :(得分: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>

答案 4 :(得分:0)

您是否已经为您的意图过滤器添加了一个类别:

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

答案 5 :(得分:0)

您可以随时尝试使用HTML发送电子邮件,然后使用<a>标记来创建网址。我认为没有办法改变Gmail或Mail解析文本的方式,因为他们可能会使用Linkify类。

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

答案 6 :(得分:0)

我也碰到了这个,但对于标准的http:scheme urls。 Gmail似乎没有向Intent添加任何类别。现在我检查BROWSABLE,但我也检查了!intent.hasCategories()并允许它也可以通过。