简体   繁体   English

从Android中的URL方案打开我的应用程序

[英]Open my app from URL scheme in Android

I know there are several questions on SO about this, but none of them are helping to resolve my issue. 我知道有关于此问题有几个问题,但没有一个问题有助于解决我的问题。

I want to be able to click a link from an email/text/browser and open my app. 我希望能够从电子邮件/文本/浏览器中单击链接并打开我的应用程序。

I have the AndroidManifest.xml code: 我有AndroidManifest.xml代码:

    <!-- Splash Activity -->
    <activity
        android:name=".ui.SplashActivity"
        android:label="@string/app_name"
        android:theme="@style/SplashTheme"
        android:launchMode="singleTask">

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

        <!-- Branch URI scheme -->
        <intent-filter>
            <data android:scheme="myscheme" android:host="open" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>

This appears to follow the documentation here exactly, however, when I use Chrome (or Android Browser) to browse to myscheme://open , my app does not open, and instead I get Google search results for myscheme://open . 这似乎完全遵循此处的文档,但是,当我使用Chrome(或Android浏览器)浏览到myscheme://open ,我的应用程序无法打开,而是我获得了myscheme:// open的 Google搜索结果。

Can anyone see what piece of the puzzle I am missing here? 任何人都能看到我在这里失踪的那块拼图吗? How can I get my app to open via URI? 如何让我的应用程序通过URI打开?

Small update: 小更新:

After reading this , I have found that navigating Chrome to market://details?id=com.myapp does not open the play store. 阅读本文后 ,我发现将Chrome导航到market://details?id=com.myapp 无法打开Play商店。 Instead it returns Google search results - the same as when trying to launch my app directly. 相反,它会返回Google搜索结果 - 与尝试直接启动我的应用时相同。 Which is confusing. 这令人困惑。 Am I missing a global OS setting to allow deep linking? 我是否缺少全局操作系统设置以允许深层链接?

Chrome does not open apps from manually-entered URI schemes. Chrome无法通过手动输入的URI方案打开应用。 In fact, a manually-entered link will never launch an external app in Chrome. 事实上,手动输入的链接永远不会在Chrome中启动外部应用。 This is by design — for whatever reason, the Chrome team feels users should always be kept inside the browser after entering an address. 这是设计 - 无论出于何种原因,Chrome团队认为用户在输入地址后应始终保持在浏览器中。

You can work around this to some extent by putting the deep link behind a user-drive action (a standard <a> tag, for example), but the Chrome has its own standard called Chrome Intents that you are supposed to use instead. 您可以通过将深层链接置于用户驱动器操作(例如标准的<a>标签)后面来解决此问题,但Chrome有自己的标准,称为Chrome Intents,您应该使用它。 Depending on the device you are using, this could also be the case for the standard browser. 根据您使用的设备,标准浏览器也可能如此。

In other apps, it's up to the app in question whether a custom URI scheme is identified as a 'clickable link'. 在其他应用程序中,有问题的应用程序是否将自定义URI方案标识为“可点击链接”。 For best compatibility, you'll want to wrap it inside a regular http:// link with a redirect. 为了获得最佳兼容性,您需要将其包含在带有重定向的常规http://链接中。

If you're interested in an in-depth explanation of the complications around deep linking, this post is a good place to start. 如果您对深层链接的复杂性有深入的解释感兴趣, 这篇文章是一个很好的起点。 If you just want it to work without a lot of extra effort, Branch.io (full disclosure: I'm on the Branch team) solves this problem as a free service. 如果你只是想让它在没有太多额外工作的情况下工作, 那么Branch.io (完全披露:我在分支团队中)将这个问题解决为免费服务。

There is no requirement for any app to pay any attention to myscheme://open . 没有要求任何应用程序关注myscheme://open

In particular: 特别是:

  • There is no requirement for any Web browser to try to launch some third-party app when you type myscheme://open into the address bar, because users are unlikely to do that in the real world. 当您输入myscheme://open地址栏时,不要求任何Web浏览器尝试启动某些第三方应用程序,因为用户不太可能在现实世界中这样做。

  • There is no requirement for an email program to try to create an ACTION_VIEW Intent for myscheme://open . 没有要求电子邮件程序尝试为myscheme://open创建ACTION_VIEW Intent

  • There is no requirement for an SMS client to search all incoming messages for the string myscheme://open , realize that this is somehow an app link, and do something with it. 没有要求SMS客户端搜索所有传入消息的字符串myscheme://open ,意识到这是某种应用程序链接,并使用它做一些事情。

You will have somewhat better luck overall using an https (or, in a pinch, http ) URL as your app link (the http://www.example.com/gizmos URL from the documentation that you linked to ). 使用https (或者,在一个紧凑的http )URL作为您的应用程序链接( 您链接到的文档中http://www.example.com/gizmos URL)总体上会有更好的运气。 Then, SMS clients might recognize that this is a URL and do something with it. 然后,SMS客户端可能会识别出这是一个URL并对其执行某些操作。 And, overall, you should be taken either to your app or to your Web page, both of which are at least somewhat useful to the user. 而且,总的来说,您应该被带到您的应用程序或您的网页,这两者至少对用户有用。 However, your app still will not necessarily open when the user types your URL into the address bar, as browsers may not expect an app link to be entered there. 但是,当用户在地址栏中输入您的网址时,您的应用仍然不一定会打开,因为浏览器可能不希望在那里输入应用链接。

You can increase the odds of an https app link working, and working the way that you want, by publishing a digital asset links file and tying that into your manifest. 您可以通过发布数字资产链接文件并将其绑定到清单中来增加https应用程序链接工作的可能性,并以您希望的方式工作。

However, overall, this is still reliant on clients opting into paying attention to this stuff. 但总的来说,这仍然依赖于客户选择关注这些东西。 Sometimes they will, because Android makes it relatively easy to do so. 有时它们会,因为Android使它相对容易。 Sometimes they will not. 有时他们不会。 A Web browser is welcome to see your URL, see that it is https , say "hey, as a Web browser, I know how to handle this!", and go load that Web page, ignoring your installed app. 欢迎使用Web浏览器查看您的URL,看到它是https ,说“嘿,作为Web浏览器,我知道如何处理这个!”,然后加载该网页,忽略您安装的应用程序。 You and I might consider that to be a bug in that browser; 您和我可能会认为这是该浏览器中的错误; the developers of that browser are welcome to disagree. 欢迎该浏览器的开发人员不同意。

I have found that navigating Chrome to market://details?id=com.myapp does not open the play store 我发现将Chrome导航到市场://详细信息?id = com.myapp无法打开Play商店

This goes back to the "developers can do what they want" part. 这可以追溯到“开发人员可以做他们想要的”部分。 If you clicked a link in a Web page to that URL, there is a chance that Chrome will take you to the Play Store. 如果您点击网页中指向该网址的链接,则Chrome可能会将您带到Play商店。 However, Chrome is not obligated to treat the address bar the same as it treats a link in a Web page. 但是,Chrome没有义务将地址栏视为处理网页中的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM