我想直接从我的应用中打开Google Play商店。
以下是我现在正在做的事情。
try {
// Check whether Google Play store is installed or not:
this.getPackageManager().getPackageInfo(
"com.android.vending", 0);
url = "market://details?id=" + packageName;
} catch (final Exception e) {
url = "https://play.google.com/store/apps/details?id="
+ packageName;
}
// 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.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://search?q=foo"));
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
for (int a = 0; a < list.size(); a++) {
ResolveInfo info = list.get(a);
ActivityInfo activity = info.activityInfo;
if (activity.name.contains("com.google.android")) {
ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
Intent i = new Intent(Intent.ACTION_MAIN,
Uri.parse("http://play.google.com/store/apps/details?id="
+ packageName));
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
getActivity().finish();
}
}
}
但现在的问题是它打开了Play商店的主页,但我希望它重定向到我发送的包名称的特定应用程序。任何人都可以帮助我。
总是感谢帮助。
答案 0 :(得分:29)
使用此代码......它适用于我
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)));
}
答案 1 :(得分:5)
如果我找对你,你正在寻找这个。
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)));
}
试试这个