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

后端 未结 23 3070
既然无缘
既然无缘 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: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 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, "");
    }
    

提交回复
热议问题