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

后端 未结 23 2720
既然无缘
既然无缘 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:18

    My kotlin entension function for this purpose

    fun Context.canPerformIntent(intent: Intent): Boolean {
            val mgr = this.packageManager
            val list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
            return list.size > 0
        }
    

    And in your activity

    val uri = if (canPerformIntent(Intent(Intent.ACTION_VIEW, Uri.parse("market://")))) {
                Uri.parse("market://details?id=" + appPackageName)
            } else {
                Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)
            }
            startActivity(Intent(Intent.ACTION_VIEW, uri))
    
    0 讨论(0)
  • 2020-11-22 02:18

    A kotlin verison with fallback and current syntax

     fun openAppInPlayStore() {
        val uri = Uri.parse("market://details?id=" + context.packageName)
        val goToMarketIntent = Intent(Intent.ACTION_VIEW, uri)
    
        var flags = Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
        flags = if (Build.VERSION.SDK_INT >= 21) {
            flags or Intent.FLAG_ACTIVITY_NEW_DOCUMENT
        } else {
            flags or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
    
        goToMarketIntent.addFlags(flags)
    
        try {
            startActivity(context, goToMarketIntent, null)
        } catch (e: ActivityNotFoundException) {
            val intent = Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName))
    
            startActivity(context, intent, null)
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:19

    Here is the final code from the answers above that first attempts to open the app using the Google play store app and specifically play store, if it fails, it will start the action view using the web version: Credits to @Eric, @Jonathan Caballero

    public void goToPlayStore() {
            String playStoreMarketUrl = "market://details?id=";
            String playStoreWebUrl = "https://play.google.com/store/apps/details?id=";
            String packageName = getActivity().getPackageName();
            try {
                Intent intent =  getActivity()
                                .getPackageManager()
                                .getLaunchIntentForPackage("com.android.vending");
                if (intent != null) {
                    ComponentName androidComponent = new ComponentName("com.android.vending",
                            "com.google.android.finsky.activities.LaunchUrlHandlerActivity");
                    intent.setComponent(androidComponent);
                    intent.setData(Uri.parse(playStoreMarketUrl + packageName));
                } else {
                    intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreMarketUrl + packageName));
                }
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreWebUrl + packageName));
                startActivity(intent);
            }
        }
    
    0 讨论(0)
  • 2020-11-22 02:20

    You can do this using the market:// prefix.

    final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
    try {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
    } catch (android.content.ActivityNotFoundException anfe) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
    }
    

    We use a try/catch block here because an Exception will be thrown if the Play Store is not installed on the target device.

    NOTE: any app can register as capable of handling the market://details?id=<appId> Uri, if you want to specifically target Google Play check the Berťák answer

    0 讨论(0)
  • 2020-11-22 02:23

    Peoples, dont forget that you could actually get something more from it. I mean UTM tracking for example. https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

    public static final String MODULE_ICON_PACK_FREE = "com.example.iconpack_free";
    public static final String APP_STORE_URI =
            "market://details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
    public static final String APP_STORE_GENERIC_URI =
            "https://play.google.com/store/apps/details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
    
    try {
        startActivity(new Intent(
            Intent.ACTION_VIEW,
            Uri.parse(String.format(Locale.US,
                APP_STORE_URI,
                MODULE_ICON_PACK_FREE,
                getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    } catch (android.content.ActivityNotFoundException anfe) {
        startActivity(new Intent(
            Intent.ACTION_VIEW,
            Uri.parse(String.format(Locale.US,
                APP_STORE_GENERIC_URI,
                MODULE_ICON_PACK_FREE,
                getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }
    
    0 讨论(0)
  • 2020-11-22 02:25

    I have combined both Berťák and Stefano Munarini answer to creating a hybrid solution which handles both Rate this App and Show More App scenario.

            /**
             * This method checks if GooglePlay is installed or not on the device and accordingly handle
             * Intents to view for rate App or Publisher's Profile
             *
             * @param showPublisherProfile pass true if you want to open Publisher Page else pass false to open APp page
             * @param publisherID          pass Dev ID if you have passed PublisherProfile true
             */
            public void openPlayStore(boolean showPublisherProfile, String publisherID) {
    
                //Error Handling
                if (publisherID == null || !publisherID.isEmpty()) {
                    publisherID = "";
                    //Log and continue
                    Log.w("openPlayStore Method", "publisherID is invalid");
                }
    
                Intent openPlayStoreIntent;
                boolean isGooglePlayInstalled = false;
    
                if (showPublisherProfile) {
                    //Open Publishers Profile on PlayStore
                    openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("market://search?q=pub:" + publisherID));
                } else {
                    //Open this App on PlayStore
                    openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("market://details?id=" + getPackageName()));
                }
    
                // find all applications who can handle openPlayStoreIntent
                final List<ResolveInfo> otherApps = getPackageManager()
                        .queryIntentActivities(openPlayStoreIntent, 0);
                for (ResolveInfo otherApp : otherApps) {
    
                    // look for Google Play application
                    if (otherApp.activityInfo.applicationInfo.packageName.equals("com.android.vending")) {
    
                        ActivityInfo otherAppActivity = otherApp.activityInfo;
                        ComponentName componentName = new ComponentName(
                                otherAppActivity.applicationInfo.packageName,
                                otherAppActivity.name
                        );
                        // make sure it does NOT open in the stack of your activity
                        openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        // task reparenting if needed
                        openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        // if the Google Play was already open in a search result
                        //  this make sure it still go to the app page you requested
                        openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        // this make sure only the Google Play app is allowed to
                        // intercept the intent
                        openPlayStoreIntent.setComponent(componentName);
                        startActivity(openPlayStoreIntent);
                        isGooglePlayInstalled = true;
                        break;
    
                    }
                }
                // if Google Play is not Installed on the device, open web browser
                if (!isGooglePlayInstalled) {
    
                    Intent webIntent;
                    if (showPublisherProfile) {
                        //Open Publishers Profile on web browser
                        webIntent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse("http://play.google.com/store/search?q=pub:" + getPackageName()));
                    } else {
                        //Open this App on web browser
                        webIntent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
                    }
                    startActivity(webIntent);
                }
            }
    

    Usage

    • To Open Publishers Profile
       @OnClick(R.id.ll_more_apps)
            public void showMoreApps() {
                openPlayStore(true, "Hitesh Sahu");
            }
    
    • To Open App Page on PlayStore
    @OnClick(R.id.ll_rate_this_app)
    public void openAppInPlayStore() {
        openPlayStore(false, "");
    }
    
    0 讨论(0)
提交回复
热议问题