如何直接从我的Android应用程序打开谷歌Play商店?

我已经打开谷歌播放商店使用以下代码

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);.

但它向我显示了一个完整的操作视图,以选择选项(浏览器/播放商店)。我需要直接在Play Store打开应用程序。

875951 次浏览

你可以使用# EYZ0前缀

Java

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 {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")))
} catch (e: ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName")))
}

我们在这里使用try/catch块,因为如果Play Store没有安装在目标设备上,Exception将被抛出。

请注意:任何应用程序都可以注册为能够处理market://details?id=<appId> URI。如果你想特别针对谷歌播放,< em >误码率ťak < / em >的答案中的解决方案是一个很好的替代方案。

使用市场:/ /

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));

获取参考在这里:

你也可以尝试这个问题公认答案中描述的方法: # EYZ0 < / p >

试试这个

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);

您可以检查是否安装了谷歌播放商店应用程序,如果是这种情况,您可以使用“市场:/ /”协议。

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);

如果你想单独打开谷歌Play(或任何其他应用程序),以上所有答案都可以在同一个应用程序的新视图中打开谷歌Play:

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);

# EYZ0

我看到的大多数答案都使用了其他答案的方法,这不是我需要的,希望这能帮助到别人。

的问候。

进入Android开发者官方链接作为教程,一步一步地从play store中查看并获得应用程序包的代码,如果存在或play store应用程序不存在,然后从web浏览器中打开应用程序。

Android开发者官方链接

https://developer.android.com/distribute/tools/promote/linking.html

链接到应用程序页面

来自网站:https://play.google.com/store/apps/details?id=<package_name>

来自Android应用:market://details?id=<package_name>

链接到产品列表

来自网站:https://play.google.com/store/search?q=pub:<publisher_name>

来自Android应用:market://search?q=pub:<publisher_name>

链接到搜索结果

来自网站:https://play.google.com/store/search?q=<search_query>&c=apps

来自Android应用:market://search?q=<seach_query>&c=apps

这里有很多答案建议使用 Uri.parse("market://details?id=" + appPackageName)) 打开谷歌Play,但我认为这是不够的事实上:

一些第三方应用程序可以使用定义了"market://"模式的自己的意图过滤器,因此他们可以处理提供的Uri,而不是谷歌播放(我经历过这种情况,例如snappea应用程序)。问题是“如何打开谷歌Play Store?”,所以我假设,你不想打开任何其他应用程序。请注意,例如,应用评级只与GP Store应用相关……

要打开谷歌Play并且只打开谷歌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);
}
}

重点是,当谷歌Play旁边有更多的应用程序可以打开我们的意图时,应用程序选择对话框被跳过,GP应用程序直接启动。

< >强更新: 有时它似乎只打开GP应用程序,而不打开应用程序的配置文件。正如TrevorWiley在他的评论中所建议的,Intent.FLAG_ACTIVITY_CLEAR_TOP可以解决这个问题。(我自己还没有测试…)

请参阅这个答案以了解Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED的功能。

现成的解决方案:

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)));
}
}


}

根据Eric的回答。

如果你想从你的应用程序打开谷歌播放商店,然后直接使用这个命令:market://details?gotohome=com.yourAppName,它将打开你的应用程序的谷歌播放商店页面。

  • 网络:# EYZ0
  • 应用:市场:/ /细节?id =

显示特定发行商的所有应用

  • 网络:# EYZ0:
  • 应用:市场:/ /搜索?q =酒吧:

搜索使用标题或描述上的查询的应用程序

  • 网络:# EYZ0
  • 应用:市场:/ /搜索?q =

参考:# EYZ0

而Eric的答案是正确的,Berťák的代码也可以工作。我认为这两者结合起来更优雅。

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 Store。如果没有安装Play Store, Exception将被捕获。

我已经结合了 贝拉克Stefano Munarini的答案,以创建一个混合解决方案,处理 给这个应用程序打分显示更多应用程序的情况。

        /**
* 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);
}
}

用法

  • 打开发布者配置文件
   @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, "");
}
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)));
}
}

我的 Kotlin 张力函数就是为了这个目的

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

在你的活动中

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))

以下是上述答案的最终代码,首先尝试使用 Google Play Store 应用程序打开应用程序,特别是 Play Store,如果失败,它将使用 Web 版本启动操作视图: 感谢“ 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);
}
}

这个链接将打开应用程序自动在 market://,如果你在安卓和浏览器,如果你在电脑上。

https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1

科特林

fun openAppInPlayStore(appPackageName: String) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName")))
} catch (exception: android.content.ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")))
}
}

非常晚在聚会 官方文件在这里。和代码描述是

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"https://play.google.com/store/apps/details?id=com.example.android"));
intent.setPackage("com.android.vending");
startActivity(intent);

在配置此意图时,将 "com.android.vending"传递给 Intent.setPackage(),以便用户在 而不是一个选择器中看到应用程序的详细信息。 KOTLIN

val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(
"https://play.google.com/store/apps/details?id=com.example.android")
setPackage("com.android.vending")
}
startActivity(intent)

如果你已经使用 Google Play Instant 发布了一个即时应用程序,你可以按以下方式启动该应用程序:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri.Builder uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
.buildUpon()
.appendQueryParameter("id", "com.example.android")
.appendQueryParameter("launch", "true");


// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using
// Activity.getIntent().getData().
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId");


intent.setData(uriBuilder.build());
intent.setPackage("com.android.vending");
startActivity(intent);

为了 KOTLIN

val uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
.buildUpon()
.appendQueryParameter("id", "com.example.android")
.appendQueryParameter("launch", "true")


// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using Activity.intent.data.
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId")


val intent = Intent(Intent.ACTION_VIEW).apply {
data = uriBuilder.build()
setPackage("com.android.vending")
}
startActivity(intent)

人民,不要忘记,你实际上可以得到更多的东西从它。我的意思是 UTM 跟踪例如。 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));
}

带有回退和当前语法的 kotlin 版本

 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)
}
}

由于 官方文件使用 https://而不是 market://,这将 Eric 和 M3-n50的答案与代码重用结合起来(不要重复) :

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

如果 GPlay 应用程序存在,它会尝试打开,然后回到默认状态。

科特林:

分机:

fun Activity.openAppInGooglePlay(){


val appId = BuildConfig.APPLICATION_ID
try {
this.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
} catch (anfe: ActivityNotFoundException) {
this.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$appId")
)
)
}}

方法:

    fun openAppInGooglePlay(activity:Activity){


val appId = BuildConfig.APPLICATION_ID
try {
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
} catch (anfe: ActivityNotFoundException) {
activity.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$appId")
)
)
}
}

这个问题的一些答案已经过时了。

根据 这个链接的说法,在2020年,对我起作用的是明确告诉用户跳过选择器,直接打开游戏商店应用程序的意图:

”如果你想从 Android 应用程序链接到你的产品,创建一个 打开 URL 的意图。在配置此意图时,传递 “ com.android.vending”转换为 Inent.setPackage () ,这样用户就可以看到您的 应用程序的详细信息在谷歌应用商店的应用程序,而不是一个选择。”

这是我用来指导用户在 Google Play 中查看包含 com.Google.android.apps.map 的应用程序的 Kotlin 代码:

val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")
setPackage("com.android.vending")
}
startActivity(intent)

我希望这对谁有帮助!

差饷申请: 转往 Playstore。 在 Flutter 中,你可以像这样通过一个平台频道来完成

颤振部分:-

 static const platform = const MethodChannel('rateApp');   // initialize

收音机: platform.invokeMethod('urls', {'android_id': 'com.xyz'}),

现在 Android 原生部分(Java) :

private static final String RATEAPP = "rateApp";  // initialize variable

//现在在 ConfigureFlutterEngine 函数中:

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), RATEAPP)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("urls") && call.hasArgument("android_id")) {
String id = call.argument("android_id").toString();
                          

try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("$uri" + id)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + id)));
}
result.success("Done");
} else {
result.notImplemented();
}
}
);

在上下文中创建扩展。

fun Context.openPlayStoreApp(pkgName:String?){
if(!pkgName.isNullOrEmpty()) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$pkgName")))
} catch (e: ActivityNotFoundException) {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$pkgName")
)
)
}
}
}

希望能成功。

测试过了,应该没问题。

val context = LocalContext.current
val onOpenPlayStore: () -> Unit = {
try {
LOG.d(tag, "onOpenPlayStore ${context.packageName}")
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=${context.packageName}"))
startActivity(context, intent, null)
} catch (e: ActivityNotFoundException) {
var intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=${context.packageName}"))
startActivity(context, intent, null)
}
}