如何在android中测试自定义URL方案 [英] How to test custom URL scheme in android
问题描述
我正在开发需要自定义网址方案"的应用,看起来像"appname://"
I'm working on app that requires Custom URL Scheme looks like "appname://"
我搜索了很多东西,还发现了许多相关问题,但是我仍然遇到麻烦.
I've searched lot of things and also found many related questions, but I'm still having trouble.
如何从浏览器测试此URL方案?当前,只要我在浏览器的地址栏中输入"appname://",它就会直接进入Google搜索.
How I can test this URL Scheme from a browser? Currently, whenever I type in a browser's address bar "appname://", it goest directly to a Google search.
我的AndroidManifest.xml中有这个文件:
I have this in my AndroidManifest.xml:
<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="appname"
android:host="appnamehost"/>
</intent-filter>
推荐答案
通过adb很容易.检查以下命令:
That is easy via adb. Check this command:
adb shell am start -a android.intent.action.VIEW -d "appname://appnamehost" your.package.name
但是,您应该使用程序包名称作为架构.因此,您可以绝对确定不存在冲突(或者详细地说是一个意图选择器).
However you should use your package name as schema. So you can be absolutely sure that there is no conflict (or in detail a intent chooser).
如果有多个设备,则需要执行adb devices
.这将产生如下输出:
If you have multiple devices you need to execute adb devices
. This will result a output like this:
List of devices attached
1645e1d0b86b device
S9WM00904386 device
然后,您可以从上方使用该ID来通过-s
参数寻址具体设备.这将导致如下所示的命令行:
Then you can use that id from above to address a concrete device with the -s
parameter. This will result a command line like this:
adb -s S9WM00904386 shell [...]
如果使用浏览器链接,这也很容易.您只需要添加以下这行html:
In case of a browser link this is also easy. You just need to add this line of html:
<a href="appname://appnamehost">Click me</a>
如果您希望在网址中使用后备广告,则可以尝试使用意图网址:
If you want a fallback in your url you could try the intent url:
<a href="intent://apphostname#Intent;scheme=appname;package=your.package.name;S.browser_fallback_url=http%3A%2F%2Fwww.example.com;end">with alternative</a>
即使您的架构不是唯一的,这也会打开您的应用程序;如果您的应用程序未安装,它将打开example.com.
This will open your app even if you schema is not unique, and if you app is not installed it will open example.com.
这篇关于如何在android中测试自定义URL方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!