Java 使用 showInstallPrompt 从即时调用可安装应用程序的语法

Java syntax for calling installable app from instant with showInstallPrompt

我正在寻找 Java 样板文件以从我的即时应用程序调用我的可安装应用程序。我在 GitHub 此处 https://github.com/googlesamples/android-instant-apps 上发现了一些用 Kotlin 编码的东西,我正在尝试翻译并开始工作。我很惊讶 Java 没有在某处提供,至少我还没有找到它。目前,我只是在 Goggle Play 上为我的可安装应用程序提供 link,用户首先会从中下载即时应用程序。比在即时应用程序上打开一个漂亮的 window 更粗糙。

这里有两个 Java 代码命令,它们将在您的免安装应用程序中打开一个 window 到 Google Play 上的可安装应用程序。我翻译了我在 GitHub 上的示例应用程序中找到的 Kotlin 代码:https://github.com/googlesamples/android-instant-apps.

Kotlin 原文有 Java 个错误:

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import com.google.android.gms.instantapps.InstantApps

Java:

import com.google.android.gms.instantapps.InstantApps;
import android.content.Intent;
import android.net.Uri;

Intent postInstallIntent = new Intent(Intent.ACTION_VIEW,
       Uri.parse("https://install-api.instantappsample.com/")).
       addCategory(Intent.CATEGORY_BROWSABLE);

InstantApps.showInstallPrompt((Activity)getContext(),//'this' if calling from main
       postInstallIntent,7,"InstallApiActivity");

Intent postInstallIntent = new Intent(Intent.ACTION_MAIN,
                            Uri.parse("https://your.package.name/")).
                            addCategory(Intent.CATEGORY_DEFAULT);

InstantApps.showInstallPrompt((Activity)getContext(),postInstallIntent,7,null);

各种参数都适用于此,尽管第二个更接近我在此处发现的 https://developer.android.com/topic/google-play-instant/getting-started/instant-enabled-app-bundle#java。奖金代码,Java 即时应用标志:

boolean isInstantApp = InstantApps.getPackageManagerCompat(getContext()).isInstantApp();

我有没有提到我有多讨厌 Kotlin?请注意,如果 Google Play 在用户设备上被禁用,这将使应用程序崩溃。我很难发现这一点,在禁用 Google Play 以测试我的应用程序页面在浏览器上的外观后,我花了数小时试图解决 'could not find activity' 错误。如果您不首先禁用 Google Play 应用程序,在 phone 浏览器上搜索 'Google Play' 会自动跳转到该应用程序,至少对我来说是这样。