通过浏览器中的URL打开Android应用 [英] Open an Android app via a URL in the browser

查看:149
本文介绍了通过浏览器中的URL打开Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我知道要做的还不止于此.我在这里阅读了许多答案,所有答案都说相同的话,但对我来说却不起作用.我希望能够使用带有来自浏览器的自定义URL方案的链接来打开Android应用.首先,我正在使用仿真器(实际上是Genymotion),所以我不知道这是否与它有任何关系.我看到的答案一直在说,我所需要的就是与此类似的东西:

Ok, so I know there has to be more to it than this. I've read many answers on here that all say the same thing but it's not working for me. I want to be able to open an Android app using a link with a custom URL scheme from the browser. First, I am using the emulator (actually Genymotion) so I don't know if that has anything to do with it. The answers I see keep saying that all I need is something similiar to this:

<intent-filter>
    <data android:scheme="myapp" android:host="hello"/>
    <action android:name="ANDROID.INTENT.ACTION.VIEW"/>
    <category android:name="ANDROID.INTENT.CATEGORY.BROWSABLE"/>
    <category android:name="ANDROID.INTENT.CATEGORY.DEFAULT"/>
</intent-filter>

我把它放在一个新的空白活动上,而不是主要活动上.

I put this on a new blank activity, not the main activity.

如果我理解正确,则任何以 myapp://hello 开头的URL都将使用该意图过滤器打开活动.但是,无论尝试什么,我都会在浏览器中不断收到ERR_UNKNOWN_URL_SCHEME.这是库存的Android浏览器,因为我不能将Chrome放在Genymotion上(至少不能在免费版本上使用).这需要Chrome吗?它只能在实际设备上完成吗?我在做错什么吗?

If I understand correctly, any URL starting with myapp://hello will open the activity with that intent filter. However, no matter what I try, I keep getting an ERR_UNKNOWN_URL_SCHEME in the browser. This is the stock Android browser as I can't put Chrome on Genymotion (at least not with the free version). Is this something that requires Chrome? Is it something that can only be done on an actual device? Is there something that I am doing wrong?

推荐答案

这里有几件事情在玩.

首先,正如Blackbelt所指出的,Android与大多数编程语言和开发环境一样,区分大小写.您需要根据这些操作和类别的文档将其更改为适当的情况:

First, as Blackbelt noted, Android is case-sensitive, as are most programming languages and development environments. You would need to change this to be the proper case, following the documentation for those actions and categories:

<intent-filter>
    <data android:scheme="myapp" android:host="hello"/>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

第二,并不是所有的Web浏览器都处理相同的方案,例如 myapp://.我不知道任何司法辖区的法律都强迫开发人员遵守这些法律.您将遇到三种基本模式:

Second, not all Web browsers handle schemes like myapp:// the same. I am not aware of any law on the books in any jurisdiction forcing developers to honor them. There are three basic patterns that you will encounter:

  • 某些浏览器可能会很好地处理它们

  • Some browsers may handle them just fine

在页面的链接中使用此类URL时,某些浏览器会处理这些URL,但如果在地址栏中键入URL,则不会处理

Some browsers will handle them when such URLs are used in links on a page, but not if the URL is typed in the address bar

某些浏览器不会尝试找到处理它们的活动,而只会一直失败

Some browsers will not attempt to find an activity to handle them and just fail all the time

Google的首选替代方法是让您使用 http https 方案,并控制主机和路径.一些浏览器将始终打开该网页,但是其他浏览器将进行检查,看到有一个活动在宣传该URL的支持,并为用户提供了处理方式的选择.Android的"M"版本将提供更多的挂钩(应用链接" )可以绕过选择器.另外,其他应用程序(例如,文本消息客户端,电子邮件客户端)通常将使 http 链接可点击,从而允许用户选择打开您的应用程序,而很少有应用程序会知道 myapp://hello 以相同的方式有意义.

Google's preferred alternative is for you to use an http or https scheme, with a host and path that you control. Some browsers will always open the Web page, but others will check, see that there is an activity that advertises support for that URL, and give the user a choice of how to handle it. The "M" version of Android will provide further hooks ("App Links") that can bypass the chooser. Plus, other apps (e.g., text messaging clients, email clients) will typically make http links clickable, allowing users the choice of opening up your app, whereas few apps will know that myapp://hello is meaningful in the same way.

这篇关于通过浏览器中的URL打开Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆