How to open the Google Play Store directly from my Android application?

后端 未结 23 3291
既然无缘
既然无缘 2020-11-22 02:00

I have open the Google Play store using the following code

Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse(\"https://play.goo         


        
23条回答
  •  天命终不由人
    2020-11-22 02:05

    While Eric's answer is correct and Berťák's code also works. I think this combines both more elegantly.

    try {
        Intent appStoreIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
        appStoreIntent.setPackage("com.android.vending");
    
        startActivity(appStoreIntent);
    } catch (android.content.ActivityNotFoundException exception) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
    }
    

    By using setPackage, you force the device to use the Play Store. If there is no Play Store installed, the Exception will be caught.

提交回复
热议问题