我使用此代码将用户发送到另一个应用程序。如果用户的手机上不存在该应用程序,我不希望用户被发送到 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/