如何直接从我的Android应用程序打开Google Play商店?
我已经打开谷歌Play商店使用follwing代码
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);.
但它显示了一个完整的行动视图select选项(浏览器/播放存储)。 我需要直接在Playstore中打开应用程序。
你可以使用market://
前缀来做到这一点。
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))); }
我们在这里使用一个try/catch
块,因为如果Play Store没有安装在目标设备上,就会抛出Exception
。
注意 :任何应用程序都可以注册为能够处理market://details?id=<appId>
Uri,如果您想专门定位Google Play,请检查Berrige答案
这里有许多答案build议使用 Uri.parse("market://details?id=" + appPackageName))
来打开Google Play,但我认为它实际上是不够的:
某些第三方应用程序可以使用自己的意图filter和"market://"
scheme定义 ,因此他们可以处理提供的Uri而不是Google Play(我遇到了egSnapPea应用程序这种情况)。 问题是“如何打开Google Play商店?”,所以我认为,你不想打开任何其他应用程序。 还请注意,例如,应用程序的评级只在GP商店应用程序等相关…
要打开Google Play和Google Play我只能使用这种方法:
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<ResolveInfo> 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); } }
重点是,当更多的应用程序旁边的Google Play可以打开我们的意图,应用程序select器对话框被跳过,GP应用程序直接启动。
更新:有时似乎只打开GP应用程序,而不打开应用程序的configuration文件。 正如TrevorWiley在他的评论中所build议的那样, Intent.FLAG_ACTIVITY_CLEAR_TOP
可以解决这个问题。 (我自己还没有testing过…)
了解Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
作用,请参阅此答案 。
继续作为教程的Android开发人员官方链接一步一步看,并从游戏商店获得您的应用程序包的代码,如果存在或玩商店应用程序不存在然后打开应用程序从Web浏览器。
Android开发者官方链接
http://developer.android.com/distribute/tools/promote/linking.html
链接到应用程序
从一个网站: http://play.google.com/store/apps/details?id=<package_name>
: http://play.google.com/store/apps/details?id=<package_name>
从Android应用程序: market://details?id=<package_name>
链接到产品列表
从一个网站: http://play.google.com/store/search?q=pub:<publisher_name>
从Android应用程序: market://search?q=pub:<publisher_name>
链接到search结果
从一个网站: http://play.google.com/store/search?q=<search_query>&c=apps
: http://play.google.com/store/search?q=<search_query>&c=apps
从Android应用程序: market://search?q=<seach_query>&c=apps
尝试这个
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=com.example.android")); startActivity(intent);
以上所有答案在同一个应用程序的新视图中打开Google Play,如果您真的想要开放Google Play(或任何其他应用程序)独立:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending"); ComponentName comp = new ComponentName("com.android.vending", "com.google.android.finsky.activities.LaunchUrlHandlerActivity"); // package name and activity launchIntent.setComponent(comp); launchIntent.setData(Uri.parse("market://details?id=com.facebook.katana")); startActivity(launchIntent);
重要的部分是,实际上打开谷歌播放或任何其他应用程序独立。
(我所看到的大部分使用其他答案的方法,而不是我所需要的,希望这可以帮助某人)
问候。
您可以检查是否安装了Google Play商店应用,如果是这种情况,则可以使用“market://”协议。
final String my_package_name = "........." // <- HERE YOUR PACKAGE NAME!! String url = ""; try { //Check whether Google Play store is installed or not: this.getPackageManager().getPackageInfo("com.android.vending", 0); url = "market://details?id=" + my_package_name; } catch ( final Exception e ) { url = "https://play.google.com/store/apps/details?id=" + my_package_name; } //Open the app page in Google Play store: final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(intent);
使用市场://
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + my_packagename));
你可以做:
final Uri marketUri = Uri.parse("market://details?id=" + packageName); startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
在这里获取参考:
您也可以尝试在这个问题的接受答案中描述的方法: 不能确定是否安装谷歌Play商店在Android设备上
即用型解决scheme:
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))); } } }
根据埃里克的回答。
虽然埃里克的回答是正确的,而伯加札克的代码也适用。 我认为这更加优雅地结合在一起。
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))); }
通过使用setPackage
,您可以强制设备使用Play商店。 如果没有安装Play商店,那么该Exception
将被捕获。
如果您想要从应用中打开Google Play商店,请使用以下命令: market://details?gotohome=com.yourAppName
: market://details?gotohome=com.yourAppName
,它会打开您应用的Google Play商店页面。
- url: http : //play.google.com/store/apps/details?id =
- App:market:// details?id =
显示特定发布商的所有应用
- url: http : //play.google.com/store/search?q=pub :
- 应用程序:市场://search?q =酒吧:
search使用标题或描述上的查询的应用程序
- url: http : //play.google.com/store/search?q =
- App:market:// search?q =
参考: https : //tricklio.com/market-details-gotohome-1/
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))); } }
我已经结合了Berťák和Stefano Munarini的答案来创build混合解决scheme,处理这个应用程序和显示更多的应用程序scheme。
/** * 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); } }
用法
- 打开发布者configuration文件
@OnClick(R.id.ll_more_apps) public void showMoreApps() { openPlayStore(true, "Hitesh Sahu"); }
- 在PlayStore上打开应用程序页面
@OnClick(R.id.ll_rate_this_app) public void openAppInPlayStore() { openPlayStore(false, ""); }