Android应用程序索引和深层链接:我一直都错了吗?

Mar*_*tti 7 android uri deep-linking android-app-indexing google-app-indexing

我正在实现应用程序索引,我想我错过了什么,但我不知道是什么.

按照指南,我将我的intent-filters添加到应用程序并实现了我的活动所需的内容.

我的应用程序的包是com.towers.hotelsclick和相关的网站(我还没有把链接rel元标记,因为我只是测试)是www.hotelsclick.com

我在指南中看到了以下intent-filter示例

<activity
  android:name="com.example.android.PetstoreActivity"
 android:label="@string/title_petstore">
    <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" />
        <data android:scheme="https" />
        <data android:host="www.examplepetstore.com" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

所以我用这种方式写了我的:

    <activity
        android:name=".ActivityForAppIndexing"
        android:label="@string/title_activity"
        android:screenOrientation="portrait" >
        <intent-filter android:label="@string/app_name">
            <data android:scheme="android-app"
                android:host="com.package.myappname" />
            <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.mywebsite.com" />
            <data android:scheme="https"
                android:host="www.mywebsite.com" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

此外,我编写了一个内容提供程序来解析URI

    <provider
        android:name=".contentproviders.HotelDetailsContentProvider"
        android:authorities="com.towers.hotelsclick" >
    </provider>
Run Code Online (Sandbox Code Playgroud)

应该如下映射URI:

    private static final String BASE_PATH = "https";
private static final String AUTHORITY = "com.towers.hotelsclick";

public static final Uri CONTENT_URI = Uri.parse("android-app://" + AUTHORITY
        + "/" + BASE_PATH);
Run Code Online (Sandbox Code Playgroud)

我正在使用以下adb命令通过命令行测试实现:

adb shell am start -a android.intent.action.VIEW -d "https://hotelsclick.com?hotel_id=135738" com.towers.hotelsclick
Run Code Online (Sandbox Code Playgroud)

它的工作原理.使用正确的活动和正确的参数自动打开应用程序.当应用程序启动时,会调用onNewIntent方法:

    protected void onNewIntent(Intent intent) {
    String action = intent.getAction();
    String data = intent.getDataString();
    if (Intent.ACTION_VIEW.equals(action) && data != null) {
        String recipeId = data.substring(data.lastIndexOf("/") + 1);
        Uri contentUri = HotelDetailsContentProvider.CONTENT_URI.buildUpon()
                .appendPath(recipeId).build();
        bundle = new Bundle();
        bundle.putString("pictureUrl", "");

        //showRecipe(contentUri);
    }
}
Run Code Online (Sandbox Code Playgroud)

(我从示例中得到了它,这就是为什么它谈论食谱而不是酒店)

但我想知道:我该怎么办?用a获取意图数据

getIntent().getData().getQuery()
Run Code Online (Sandbox Code Playgroud)

或使用ContentProvider并从URI中获取它们?

另外,如果一切正常,我想使用Google Search Console和"查看为Google"功能进行测试.我上传了我的APK并看到了一个URI.

现在,在文档的"测试你的应用程序"部分,我看到像http://hotelsclick.com?hotel_id=135738这样的链接被称为"深层链接",而像android-app://com.towers这样的链接. hotelsclick/https/hotelsclick.com?hotel_id = 135738被称为"URI".我对吗?

"查看为谷歌"功能要求提供URI,但我不知道如何创建一个.此外,我想知道这个URI是如何传递给应用程序的,我很确定我对这个所有URI/deepLink事情感到非常困惑,并且无法在文档中找到解脱.

我应该在何时何地使用URI?我应该在何时何地使用deepLink?我想,因为我要将链接rel = URI放在我的网页中,然后应用程序将使用URI索引应用程序,而不是使用页面的URL.但是如果是这样的话...为什么adb shell启动-a android.intent.action.VIEW -d命令用于测试URL而不是URI?我的应用程序应该能够获取和处理URI或URL吗?如何?

我以为我已经了解了应用程序索引的一些内容,但是今天我开始认为自从我开始以来我弄错了.这就是为什么我请求你帮助回答上面的问题,并且可能会对所有这些URL/URI/intent-filter/ContentProvider混乱提供帮助.

谢谢大家

reg*_*isd 5

首先,确保您的应用正确处理您的网址:

adb shell am start \
  -a android.intent.action.VIEW \
  -d 'https://www.hotelsclick.com?hotel_id=135738'
Run Code Online (Sandbox Code Playgroud)

还要检查是否可以使用可浏览类别打开活动:

adb shell am start \
  -a android.intent.action.VIEW \
  -c android.intent.category.BROWSABLE
  -d 'https://www.hotelsclick.com?hotel_id=135738'
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请编辑您的意图过滤器,使其具有类别DEFAULT和BROWSABLE,如下所示

<activity
  android:name="com.towers.hotelsclick.HotelActivity"
  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" />
        <data android:scheme="https" />
        <data android:host="www. hotelsclick.com" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

检查android-app链接是否正确处理(无处可执行),在手机上打开 android-app://com.towers.hotelsclick/https/www.hotelsclick.com?hotel_id=135738

android-app://com.towers.hotelsclick/https/www.hotelsclick.com?hotel_id=135738

如果可行,那么您唯一需要做的就是添加HTML页面

<link rel="alternate"
      href="android-app://com.towers.hotelsclick/https/www.hotelsclick.com?hotel_id=135738" />
Run Code Online (Sandbox Code Playgroud)

这告诉Google,该页面可以在标识的应用程序中打开,并且com.towers.hotelsclick有意查看https://www.hotelsclick.com/?hotel_id=135738(您刚刚测试过的内容).