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

标签 java android android-intent android-instant-apps android-app-bundle

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

最佳答案

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

带有 Java 错误的 Kotlin 原始版本:

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

enter image description here

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 以测试我的应用程序页面在浏览器上的外观后,我花了数小时试图解决“找不到 Activity ”错误。如果您不先禁用 Google Play 应用程序,则在手机浏览器上搜索“Google Play”会自动将您跳转到该应用程序,至少对我来说是这样。

关于使用 showInstallPrompt 从即时调用可安装应用程序的 Java 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57778716/

相关文章:

java - JWebBrowser在其他计算机上不起作用

java - Groovy JsonBuilder : using field names that are variable names too

android - 删除在我的情况下未放入堆栈的 fragment

java - Toast 在真实设备上不起作用,但在模拟器上工作正常

android - AOSP建筑: Make file shared libraries issue

android - phonegap 3.3.0 中的本地通知

android - 如何在 android 中使用 intent 传递 parcelable 变量值?

android - 如何将任何对象从 Activity 传递到服务?

android - 在服务和 Activity 之间传递参数

java - 空指针访问 : The variable can only be null at this location