我已经使用以下代码打开了 Google 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 中打开应用程序。
您可以使用 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)));
}
科特林
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 商店,则会抛出 Exception
。
注意:任何应用都可以注册为能够处理 market://details?id=<appId>
Uri,如果您想专门针对 Google Play,请查看 Berťák 答案
这里的很多答案建议使用 Uri.parse("market://details?id=" + appPackageName))
打开Google Play,但我认为它实际上是不够的:
一些第三方应用程序可以使用自己定义的 "market://"
方案的意图过滤器,因此它们可以处理提供的 Uri 而不是 Google Play(我在 egSnapPea 应用程序中遇到过这种情况)。问题是“如何打开 Google Play 商店?”,所以我假设您不想打开任何其他应用程序。另请注意,例如应用评级仅与 GP Store 应用等相关...
要打开 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 之外的更多应用程序可以打开我们的 Intent 时,会跳过 app-chooser 对话框,直接启动 GP 应用程序。
更新: 有时它似乎只打开 GP 应用程序,而不打开应用程序的配置文件。正如 TrevorWiley 在评论中所建议的那样,Intent.FLAG_ACTIVITY_CLEAR_TOP
可以解决问题。 (我自己还没有测试过......)
请参阅 this answer 了解 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
的作用。
rateIntent.setPackage("com.android.vending")
来确保 PlayStore 应用程序将处理这个意图,而不是所有这些代码,会发生什么?
继续 Android Developer 官方链接作为教程一步一步地查看并从 Play 商店获取您的应用程序包的代码(如果存在或 Play 商店应用程序不存在)然后从 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
market
,您将找不到任何解决方案。我认为新方法是触发更通用的意图 developer.android.com/distribute/marketing-tools/… 。较新版本的 Play 商店应用程序可能具有此 URI https://play.google.com/store/apps/details?id=com.example.android
的意图过滤器
虽然 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 商店。如果未安装 Play 商店,则会捕获 Exception
。
https://play.google.com/store/apps/details?id=
而不是 market:
怎么会? developer.android.com/distribute/marketing-tools/… 仍然是一个全面而简短的答案。
尝试这个
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");
// 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);
重要的部分是实际打开 google play 或任何其他应用程序独立。
我所看到的大部分内容都使用其他答案的方法,这不是我所需要的,希望这对某人有所帮助。
问候。
this.cordova
是什么?变量声明在哪里? callback
在哪里声明和定义?
您可以检查是否安装了 Google Play Store 应用,如果是这样,您可以使用“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));
由于 the official docs 使用 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")
)
)
}
}
你可以做:
final Uri marketUri = Uri.parse("market://details?id=" + packageName);
startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
获取参考 here:
您还可以尝试此问题的已接受答案中描述的方法:Cannot determine whether Google play store is installed or not on Android device
聚会很晚Official docs到了。描述的代码是
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()
,以便用户在 Google Play 商店应用而不是选择器中看到您应用的详细信息。对于科特林
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);
对于科特林
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)
Uri.parse("https://play.google.com/store/apps/details?id=
。在某些设备上,它会打开网络浏览器而不是 Play Market。
这个问题的一些答案已经过时了。
根据this link,对我有用的是(在 2020 年)明确告知意图跳过选择器并直接打开 Play 商店应用程序:
“如果您想从 Android 应用程序链接到您的产品,请创建一个打开 URL 的 Intent。在配置此 Intent 时,将“com.android.vending”传递给 Intent.setPackage(),以便用户在Google Play 商店应用程序,而不是选择器。”
这是我用来引导用户在 Google Play 中查看包含包名 com.google.android.apps.maps 的应用的 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)
我希望对某人有所帮助!
即用型解决方案:
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)));
}
}
}
根据埃里克的回答。
科特林
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")))
}
}
如果您使用的是 Android,此链接将在 market://
中自动打开该应用程序;如果您使用的是 PC,则该链接将在浏览器中自动打开。
https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1
https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1
如果您想从您的应用打开 Google Play 商店,请直接使用此命令:market://details?gotohome=com.yourAppName
,它将打开您应用的 Google Play 商店页面。
网站:http://play.google.com/store/apps/details?id=
应用:market://details?id=
显示特定发布者的所有应用
网站:http://play.google.com/store/search?q=pub:
应用程序:market://search?q=pub:
搜索在标题或描述上使用 Query 的应用
网址:http://play.google.com/store/search?q=
应用:market://search?q=
参考:https://tricklio.com/market-details-gotohome-1/
这是上面答案的最终代码,首先尝试使用 Google Play 商店应用程序打开应用程序,特别是 Play 商店,如果失败,它将使用网络版本启动操作视图:感谢 @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);
}
}
我结合了 Berťák 和 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 entension 函数
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))
人民,不要忘记你实际上可以从中得到更多的东西。例如,我的意思是 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)
}
}
对于费率应用:重定向到 Playstore。在 Flutter 中,你可以通过 Platform 通道来做到这一点,就像这样
颤振部分:-
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();
}
}
);
KOTLIN:在上下文中创建扩展。
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")
)
)
}
}
}
希望它应该工作。
market://search?q=pub:"+devName
和http://play.google.com/store/search?q=pub:"+devName
appPackageName
实际上是BuildConfig.APPLICATION_ID
。没有Context
/Activity
依赖项,降低了内存泄漏的风险。