我已经打开谷歌播放商店使用以下代码
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打开应用程序。
我已经打开谷歌播放商店使用以下代码
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://前缀来做到这一点。
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没有安装在目标设备上,则会抛出异常。
注意:任何应用程序都可以注册为能够处理市场://details?id = < appId > URI。如果你想特别针对谷歌Play, Berťák的答案中的解决方案是一个很好的替代方案。
使用市场:/ /
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));
在这里获取参考资料:
你也可以尝试这个问题公认答案中描述的方法: 无法确定谷歌播放商店是否安装在Android设备上
试试这个
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
您可以检查谷歌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);
如果你想单独打开谷歌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);
重要的部分是实际上打开谷歌播放或任何其他应用程序独立。
我看到的大多数答案都使用了其他答案的方法,这不是我需要的,希望这能帮助到别人。
的问候。
进入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 =酒吧:< publisher_name >
链接到搜索结果
从网站:https://play.google.com/store/search?q=<search_query>&c=apps
从Android应用程序:market://search?q = < seach_query >和c =应用
这里的许多答案都建议使用Uri.parse("market://details? "id=" + appPackageName))来打开谷歌Play,但我认为它实际上是不够的:
一些第三方应用程序可以使用定义了“market://”方案的自己的意图过滤器,因此他们可以处理提供的Uri而不是谷歌Play(我在例如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在他的评论中所说,意图。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,它将打开你的应用程序的谷歌播放商店页面。
网站:http://play.google.com/store/apps/details?id= 应用:市场:/ /细节?id =
显示特定发行商的所有应用
网站:http://play.google.com/store/search?q=pub 应用:市场:/ /搜索?q =酒吧:
搜索使用标题或描述上的查询的应用程序
网站:http://play.google.com/store/search?q= 应用:市场:/ /搜索?q =
参考:https://tricklio.com/market-details-gotohome-1/
而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,异常将被捕获。
我结合了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) 公共无效showMoreApps() { openPlayStore(true,“Hitesh Sahu”); }
在PlayStore上打开应用页面
@OnClick (R.id.ll_rate_this_app) 公共无效openAppInPlayStore() { openPlayStore(假," "); }
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))
以下是上述答案的最终代码,第一次尝试使用谷歌播放商店应用程序打开应用程序,特别是播放商店,如果失败,它将使用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);
}
}
这个链接将自动打开应用程序在市场://如果你是在Android和浏览器,如果你是在PC上。
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.”自动售卖”到Intent.setPackage()中,以便用户在谷歌Play Store应用程序中看到应用程序的详细信息,而不是选择器。 在芬兰湾的科特林
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)
如果您已经使用谷歌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应用程序存在,它会尝试打开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。自动售货”到Intent.setPackage()中,以便用户看到您的 应用程序的详细信息在谷歌Play Store应用程序,而不是一个选择器。”
这是我用来指导用户在谷歌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)
我希望这能帮助到一些人!
对于费率应用:重定向到播放商店。 在Flutter中,你可以通过这样的平台通道进行操作
颤振部分:
static const platform = const MethodChannel('rateApp'); // initialize
onTap:平台。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)
}
}