可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have open the google play store using the follwing code
Intent i = new Intent(android.content.Intent.ACTION_VIEW); i.setData(Uri.parse("https://play.google.com/store/apps/details?id=my packagename ")); startActivity(i);.
But it shows me a Complete Action View as to select the option (browser/play store). I need to open the application in playstore directly.
回答1:
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=
Uri, if you want to specifically target Google Play check the answer
回答2:
Many answers here suggest to use Uri.parse("market://details?id=" + appPackageName))
to open Google Play, but I think it is insufficient in fact:
Some third-party applications can use its own intent-filters with "market://"
scheme defined, thus they can process supplied Uri instead of Google Play (I experienced this situation with e.g.SnapPea application). The question is "How to open the Google Play Store?", so I assume, that you do not want to open any other application. Please also note, that e.g. app rating is only relevant in GP Store app etc...
To open Google Play AND ONLY Google Play I use this method:
public static void openAppRating(Context context) { // you can also use BuildConfig.APPLICATION_ID String appId = context.getPackageName(); Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appId)); boolean marketFound = false; // find all applications able to handle our rateIntent final List otherApps = context.getPackageManager() .queryIntentActivities(rateIntent, 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 rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // task reparenting if needed rateIntent.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 rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this make sure only the Google Play app is allowed to // intercept the intent rateIntent.setComponent(componentName); context.startActivity(rateIntent); marketFound = true; break; } } // if GP not present on device, open web browser if (!marketFound) { Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+appId)); context.startActivity(webIntent); } }
The point is that when more applications beside Google Play can open our intent, app-chooser dialog is skipped and GP app is started directly.
UPDATE: Sometimes it seems that it opens GP app only, without opening the app's profile. As TrevorWiley suggested in his comment, Intent.FLAG_ACTIVITY_CLEAR_TOP
could fix the problem. (I didn't test it myself yet...)
See this answer for understanding what Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
does.
回答3:
Go on Android Developer official link as tutorial step by step see and got the code for your application package from play store if exists or play store apps not exists then open application from web browser.
Android Developer official link
http://developer.android.com/distribute/tools/promote/linking.html
Linking to a Applicaiton Page
From a web site: http://play.google.com/store/apps/details?id=
From an Android app: market://details?id=
Linking to a Product List
From a web site: http://play.google.com/store/search?q=pub:
From an Android app: market://search?q=pub:
Linking to a Search Result
From a web site: http://play.google.com/store/search?q=&c=apps
From an Android app: market://search?q=&c=apps
回答4:
try this
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=com.example.android")); startActivity(intent);
回答5:
All of the above answers open Google Play in a new view of the same app, if you actually want to open Google Play (or any other app) independently:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending"); // package name and activity ComponentName comp = new ComponentName("com.android.vending", "com.google.android.finsky.activities.LaunchUrlHandlerActivity"); launchIntent.setComponent(comp); // sample to open facebook app launchIntent.setData(Uri.parse("market://details?id=com.facebook.katana")); startActivity(launchIntent);
The important part is that actually opens google play or any other app independently.
Most of what I have seen uses the approach of the other answers and it was not what I needed hopefully this helps somebody.
Regards.
回答6:
You can check if the Google Play Store app is installed and, if this is the case, you can use the "market://" protocol.
final String my_package_name = "........." //
回答7:
use market://
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + my_packagename));
回答8:
You can do:
final Uri marketUri = Uri.parse("market://details?id=" + packageName); startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
get Reference here:
You can also try the approach described in the accepted answer of this question: Cannot determine whether Google play store is installed or not on Android device
回答9:
Ready-to-use solution:
public class GoogleServicesUtils { public static void openAppInGooglePlay(Context context) { final String appPackageName = context.getPackageName(); try { context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); } catch (android.content.ActivityNotFoundException e) { // if there is no Google Play on device context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); } } }
Based on Eric's answer.
回答10:
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.
回答11:
If you want to open Google Play store from your app then use this command directy: market://details?gotohome=com.yourAppName
, it will open your app's Google Play store pages.
Show all apps by a specific publisher
Search for apps that using the Query on its title or description
Reference: https://tricklio.com/market-details-gotohome-1/
回答12:
public void launchPlayStore(Context context, String packageName) { Intent intent = null; try { intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id=" + packageName)); context.startActivity(intent); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName))); } }
回答13:
I have combined both 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, ""); }
回答14:
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))
回答15:
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); } }