Android 深层链接意图不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24808777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:47:36  来源:igfitidea点击:

Deep-linking intent does not work

androidandroid-intentandroid-manifestdeep-linking

提问by Mahoni

I followed the insttructions on https://developer.android.com/training/app-indexing/deep-linking.html, but when I want to trigger the intent through adbwith:

我遵循了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 }

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?

我有没有犯任何明显的错误?

回答by Simas

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). E.g.:

然后要接受多个数据标签,您需要不同的意图过滤器(这就是它对我有用的方式,与我在网上看到的所有其他示例不同)。例如:

<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>

NOTEthat 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. This helped: Source

我不确定为什么 Google 的 Docs 如此具有误导性,或者可能是针对某些不同版本的 adb,但上述更改对我来说非常有效。这有帮助:来源



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>

NOTEThe 1st filter works on Google Chrome while the 2nd one works on the ADB.

注意第一个过滤器适用于 Google Chrome,而第二个过滤器适用于 ADB。

NOTE2The app choice menu won't be shown if the link is entered into the browser's address bar. It hasto be a <a href="http://example.com"></a>link in side some page.

NOTE2如果将链接输入到浏览器的地址栏中,则不会显示应用程序选择菜单。它必须<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.

在我看来,这里的一切都相当模糊,而且真的不是我期望的那样。但这就是它在我的设备上的工作方式。希望这对您也有帮助(并有效)。

回答by Ferran Maylinch

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". The same with "myschema://example/whatever".

单击浏览器中的任何链接(例如“ http://www.example.com”、“ http://www.example.com/”或“ http://www.example.com/whatever”)时,这会起作用。与“myschema://example/whatever”相同。

Also works with adbusing this command (with any of those URLs):

也适用于adb使用此命令(使用任何这些 URL):

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 pathPrefixfor different activities.

当一切正常时,您可能希望pathPrefix为不同的活动配置不同的。

回答by Harry Aung

In my case, I was putting deep linking intent filter in MainActivity which is also main launcher. That caused the problem.

就我而言,我在 MainActivity 中放置了深层链接意图过滤器,它也是主启动器。这导致了问题。

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.

在我创建了另一个单独的活动并将意图过滤器放在那里之后,它解决了这个问题。希望这可以帮助面临同样问题的其他人。

回答by jdowdell

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" , 等等。}

回答by Kelsey

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. With the above you can now put it in an src, or an href, like so:

adb 工具不需要这种格式。有了上面的内容,您现在可以将其放入 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>

回答by manmohan

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

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

回答by user2600802

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.

它会起作用。

回答by amorenew

In my case I have a port 8075 in the URL I removed it and it worked

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

回答by Bachiri Taoufiq Abderrahman

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

  • You will need to test your deeplinks ,after adding the intent filter to your AndroidManifest.xml file (lines are below):

    ... ...

  • 使用此命令测试您的活动后:

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

  • 在将意图过滤器添加到您的 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

回答by Abolfazl Miadian

if you have not permission issue , it is probably related to API level

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