简体   繁体   English

深层链接意图不起作用

[英]Deep-linking intent does not work

I followed the insttructions on https://developer.android.com/training/app-indexing/deep-linking.html , but when I want to trigger the intent through adb with:我遵循了https://developer.android.com/training/app-indexing/deep-linking.html 上的说明,但是当我想通过adb触发意图时:

adb shell am start
           -W -a android.intent.action.BROWSEABLE
           -d "http://example.com/gizmos" com.myapp.android

I just get我只是得到

Error: Activity not started, unable to resolve Intent { act=android.intent.action.VIEW dat=example://gizmos flg=0x10000000 pkg=com.myapp.android }

<activity
        android:name=".activities.DeepLinkActivity"
        android:label="@string/title_activity_deep_link">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

        <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="http"
                android:host="example.com"
                android:pathPrefix="/gizmos" />
        </intent-filter>
    </activity>

Have I made any obvious mistakes?我有没有犯任何明显的错误?

EDIT:编辑:

Ok first make sure that your package is reachable by adb:好的,首先确保您的包可以通过 adb 访问:

adb shell am start -n com.example.simon.test/.activities.MainActivity

Then to accept multiple data tags you need different intent filters ( that's the way it worked for me unlike all the other examples I've seen on the net ).然后要接受多个数据标签,您需要不同的意图过滤器(这就是它对我有用的方式,与我在网上看到的所有其他示例不同)。 Eg:例如:

<intent-filter>
    ...
    <data android:scheme="http"
          android:host="example.com"/>
</intent-filter>
<intent-filter>
    ...
    <data android:scheme="http"
          android:host="example.com"
          android:pathPrefix="/gizmos"/>
</intent-filter>

NOTE that in the above example the pathPrefix starts with a forward slash !请注意,在上面的示例中,pathPrefix 以正斜杠开头!

I am not sure why Google's Docs are so misleading or maybe that was for some different version of adb, but the above changes worked perfectly for me.我不确定为什么 Google 的 Docs 如此具有误导性,或者可能是针对某些不同版本的 adb,但上述更改对我来说非常有效。 This helped: Source这有帮助:来源


This is how I made the Chrome browser route specific links to my app:这就是我如何让 Chrome 浏览器路由到我的应用程序的特定链接:

<activity
    android:name=".activities.DeepLinkActivity"
    android:label="@string/app_name">
    <!-- Accept chrome links -->
    <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="http"
              android:host="example.com"
            android:pathPrefix="/"/>
    </intent-filter>
    <!-- Accept adb data flag -->
    <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="http"
              android:host="example.com"/>
    </intent-filter>
</activity>

NOTE The 1st filter works on Google Chrome while the 2nd one works on the ADB.注意第一个过滤器适用于 Google Chrome,而第二个过滤器适用于 ADB。

NOTE2 The app choice menu won't be shown if the link is entered into the browser's address bar. NOTE2如果将链接输入到浏览器的地址栏中,则不会显示应用程序选择菜单。 It has to be a <a href="http://example.com"></a> link in side some page.必须是某个页面中的<a href="http://example.com"></a>链接。

In my opinion everything here is rather blurry and really not how I expected it all to work.在我看来,这里的一切都相当模糊,而且真的不是我期望的那样。 But that's how it works on my device.但这就是它在我的设备上的工作方式。 Hope this helps (and works) for you too.希望这对您也有帮助(并有效)。

After some tests this is what worked for me:经过一些测试,这对我有用:

    <activity android:name=".activities.MainActivity">
        <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http"
                  android:host="www.example.com"
                  android:pathPrefix=""
                />
            <data android:scheme="myschema"
                  android:host="example"
                  android:pathPrefix=""
                />
        </intent-filter>
    </activity>

This works when clicking any link in the browser such as " http://www.example.com ", " http://www.example.com/ " or " http://www.example.com/whatever ".单击浏览器中的任何链接(例如“ http://www.example.com ”、“ http://www.example.com/ ”或“ http://www.example.com/whatever ”)时,这会起作用。 The same with "myschema://example/whatever".与“myschema://example/whatever”相同。

Also works with adb using this command (with any of those URLs):还可以使用此命令(使用任何这些 URL)与adb使用:

adb shell am start -W -a android.intent.action.VIEW -d "http://www.example.com" com.example

Hope it helps to get you started.希望它可以帮助您入门。

When everything is working you will probably want to configure a different pathPrefix for different activities.当一切正常时,您可能希望为不同的活动配置不同的pathPrefix

In my case, I was putting deep linking intent filter in MainActivity which is also main launcher.就我而言,我在 MainActivity 中放置了深层链接意图过滤器,它也是主启动器。 That caused the problem.这导致了问题。

After I created another separate activity and put intent filter there, it solved the problem.在我创建了另一个单独的活动并将意图过滤器放在那里之后,它解决了这个问题。 Hope this can help others who are facing the same issue.希望这可以帮助面临同样问题的其他人。

In my case, I am using an emulator and not an actual usb connected device, and hence I had to change -d to -e, like so :就我而言,我使用的是模拟器而不是实际的 USB 连接设备,因此我必须将 -d 更改为 -e,如下所示:

adb shell am start -W -a android.intent.action.VIEW -e "http://www.example.com" com.example

Note the -e before the deep link.注意深层链接前的-e

First, read @user3249477's answer on this page.首先,阅读@user3249477 在此页面上的回答。

I just wanted to add that instead of what he wrote, you can condense it by using pathPattern:我只是想补充一点,而不是他写的,你可以使用 pathPattern 压缩它:

<activity
    android:name=".activities.DeepLinkActivity"
    android:label="@string/app_name">
    <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="http"
              android:host="example.com"
            android:pathPattern=".*"/>
    </intent-filter>
</activity>

The ".*" matches both the empty string and "/", so both cases are covered. ".*" 匹配空字符串和 "/",所以这两种情况都包含在内。 If you end up trying to handle multiple scheme's and host's this becomes especially important, so you don't have to spam a powerset of {http, https} X {"", "/"} X {"foo", "bar", etc.}如果您最终尝试处理多个方案和主机,这变得尤为重要,因此您不必垃圾邮件 {http, https} X {"", "/"} X {"foo", "bar" , 等等。}

Make sure your URL is in this format (with the cap letters replaced by your own):确保您的网址采用以下格式(大写字母替换为您自己的格式):

android-app://COM.YOUR.APP.IDENTIFIER/SCHEME/HOST?somegetparams android-app://COM.YOUR.APP.IDENTIFIER/SCHEME/HOST?somegetparams

The adb tool does not require this format. adb 工具不需要这种格式。 With the above you can now put it in an src, or an href, like so:有了上面的内容,您现在可以将其放入 src 或 href 中,如下所示:

<iframe src="android-app://COM.YOUR.APP.IDENTIFIER/SCHEME/HOST?somegetparams"> </iframe> 
<a href="android-app://COM.YOUR.APP.IDENTIFIER/SCHEME/HOST?somegetparams">LINK</a>

在我的情况下,我在 URL 中有一个端口 8075,我删除了它并且它工作正常

./adb shell am start -n packagename/.splash.SplashActivity -d "schemeName://content"

Use adb shell am start -W -a android.intent.action.VIEW -d " http://example.com/gizmos " com.myapp.android使用 adb shell am start -W -a android.intent.action.VIEW -d " http://example.com/gizmos " com.myapp.android

it will work.它会起作用。

Thanks Simas for your response i would like to add some clarification:感谢 Simas 的回复,我想补充一些说明:

  • after testing your activity with this command:使用此命令测试您的活动后:

    adb shell am start -n com.example.simon.test/.activities.MainActivity adb shell am start -n com.example.simon.test/.activities.MainActivity

  • You will need to test your deeplinks ,after adding the intent filter to your AndroidManifest.xml file (lines are below):在将意图过滤器添加到您的 AndroidManifest.xml 文件后,您将需要测试您的深层链接(以下几行):

    ... ... ……

so this is the adb command with which you can test :所以这是您可以测试的 adb 命令:

adb shell am start -W -a android.intent.action.VIEW -d "http://example.com/gizmos" com.example.packageid

and

adb shell am start -W -a android.intent.action.VIEW -d "http://example.com" com.example.pakageid

如果您没有权限问题,则可能与API级别有关

Use this intent filter instead,改用这个意图过滤器,

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<!-- Accepts URIs that begin with "example://gizmos” -->
<data
   android:host="gizmos"
   android:scheme="example" />
</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" />

<!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
<data 
    android:host="www.example.com"
    android:pathPrefix="gizmos"
    android:scheme="http" />
</intent-filter>

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

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