首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用showInstallPrompt从即时调用可安装应用的Java语法

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

Stack Overflow用户
提问于 2019-09-04 05:04:19
回答 1查看 192关注 0票数 1

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-05 04:44:53

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

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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");

代码语言:javascript
复制
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即时应用标志:

代码语言:javascript
复制
boolean isInstantApp = InstantApps.getPackageManagerCompat(getContext()).isInstantApp();

我有提过我有多讨厌Kotlin吗?注意:如果Google Play在用户设备上被禁用,这将使应用程序崩溃。我费了好大劲才发现这一点,在禁用Google Play测试我的应用程序页面在浏览器上的外观后,我花了几个小时试图解决“找不到活动”的错误。如果你不先禁用Google Play应用程序,在手机浏览器上搜索“Google Play”会自动弹出该应用程序,至少对我来说是这样。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57778716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档