【问题标题】:Android how to create Custom URL scheme with the given format myapp://http://Android 如何使用给定的格式 myapp://http:// 创建自定义 URL 方案
【发布时间】:2013-08-07 11:43:49
【问题描述】:

我已经研究了大部分自定义 URL 方案问答,但没有找到可能的答案。

我希望通过单击浏览器中的某个 URL(移动设备上的任何 URL)来启动我的应用程序,问题是我的给定 URL 不能修改,因为它也服务于 IOS 应用程序,它看起来像这样:

"myapp://http://www.name.com/path/path2/"

我不确定如何处理“myapp://http://”并构造一个适当的意图过滤器,我尝试的一切都不起作用。 任何帮助将不胜感激,如果我错过了相关答案,请除了我的道歉。

这是我迄今为止尝试过的:

      <activity
        android:name="com.myapp.test.SplashScreen"
        android:exported="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

        <!-- Test for URL scheme -->
        <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.name.com"
                android:path="/path/path2/"
                android:scheme="http" />
            <data
                android:host="www.name.com"
                android:path="/path/path2/"
                android:scheme="https" />

            <data android:scheme="myapp" />
        </intent-filter>
        <!-- End Test for URL scheme -->
    </activity>

注意:我试过有/没有exported:true

【问题讨论】:

  • 由于这不是一个有效的Uri 结构,我非常怀疑它是否会起作用。更改 iOS 应用程序以使用有效的 URI 会更简单。
  • @CommonsWare 嘿,感谢您的评论。这种URI适用于IOS应用程序,他们通过点击这个链接来打开应用程序。你认为只有“myapp://”或“http://”这样的方案吗?
  • "你认为只有一种方案,如 "myapp://" 或 "http://" 吗?" - 是的。充其量,如果幸运的话,http:// 将被视为主机。在最坏的情况下,您的 Uri 将无法解析。我可以想到其他一些选择。 Uri 中绝对只有一个方案——Uri 类不允许超过一个。
  • 我对 RFC 2396 的解读是,他定义了一个不透明的 URI 方案,而方案特定部分看起来像另一个 URI 的事实与 URI 解析器无关。 (此外,建议“只需更改您的 iOS 应用程序”有点花言巧语且无济于事,可能他需要保持兼容性的野外代码。)

标签: android url android-manifest intentfilter url-scheme


【解决方案1】:

正如 CommonsWare 所说,在我需要创建方案时给定的 URI 不是有效的 URI,因此方案不起作用并且应用程序没有启动。在这个解释之后,服务器端的人被说服将 URI 更改为 myapp://... 它就像魔术一样工作:)。

Activity 现在看起来像这样:

 <activity
    android:name="com.myapp.test.SplashScreen"
    android:exported="true"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

    <!-- Test for URL scheme -->
    <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>
    <!-- End Test for URL scheme -->
</activity>

【讨论】:

  • 这似乎是一个简单的过程,但是,它不适用于我
  • @MazenKasser 嘿,你的计划看起来怎么样?如果你把它贴在这里也许我可以帮忙
  • myapp:// 在默认浏览器中导航到应用程序。但在 chrome 中它没有打开应用程序。
  • @madanV chrome 去哪里而不是打开应用程序?
  • 重要提示:在浏览器地址栏中手动输入 urlscheme 并按回车键加载它不起作用。如果您单击带有该 urlscheme 的 URL,它确实有效!
【解决方案2】:

这是对 URI 方案的滥用,是无效的。您要传递的 HTTP URL 是一段数据,因此应该在查询字符串中发送。

myapp://somehost/action?url=http%3A%2F%2Fwww.name.com%2Fpath%2Fpath2%2F

【讨论】:

  • 他说他的given URL cannot be modified as it serves IOS app as well
【解决方案3】:

您需要使用超链接来启动应用程序。例如,您设置了 scheme="yourpackagename" ,您需要设置一个像这样的超链接:yourpackagename://host ,并且您应该在移动浏览器上查看超链接。如果您没有主机标签,只需将其删除。

<!-- Test for URL scheme -->
<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="host" android:scheme="yourpackagename" />
</intent-filter>
<!-- End Test for URL scheme -->

如果你的activity有多个scheme,你应该使用2个或更多来指定它

【讨论】:

  • android:host="" 产生错误:“android:host 不能为空”
  • 感谢您的评论,在工作室中,如果您不需要“host”,只需删除此代码 // android:host=""
猜你喜欢
  • 2013-12-10
  • 1970-01-01
  • 2011-04-30
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多