java - 将用户发送到应用程序。如果应用程序不存在发送到Playstore

标签 java android-studio android-intent start-activity

我使用此代码将用户发送到另一个应用程序。如果用户的手机上不存在该应用程序,我不希望用户被发送到 Playstore。我正在寻找示例,但没有找到任何内容。

// Launch My App one after clicking the button1
public void launchAppOne(View view) {
    Intent launchAppOne= getPackageManager().getLaunchIntentForPackage("com.app.android.myapp1");
    startActivity(launchAppOne);
}
// Launch My A after clicking the button2
public void launchAppTwo(View view) {
    Intent launchAppTwo = getPackageManager().getLaunchIntentForPackage("com.app.android.myapp2");
    startActivity(launchAppTwo);
}

最佳答案

您可以使用此代码。它会尝试启动该应用程序,如果该应用程序不存在,则会打开该应用程序的 Playstore 页面。

String packageName = "org.mozilla.firefox";
Intent intent= getPackageManager().getLaunchIntentForPackage(packageName);
if (intent != null){
    startActivity(intent);
}else{
    try {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)));
    }catch (android.content.ActivityNotFoundException anfe) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
    }
}

关于java - 将用户发送到应用程序。如果应用程序不存在发送到Playstore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52391257/

相关文章:

java - org.json.JSONObject 类型的响应无法转换为 JSONArray

java - Java 版纸牌克朗代克

java - 处理 fragment 中的单选组和按钮

android - 销毁 Activity 后调用 onStartCommand

java - 如何停止线程 - Java

Android Studio,无法导入任何东西

java - Android 5.0.1 更新时出现 Unsatisfiedlinkerror

android - Android Studio 中的 key 存储路径是什么?

android - 如何在使用 emailIntent 发送电子邮件后自动隐藏键盘

android - 使用 Intent 以 mp4 格式捕获视频 - Android