How to open Google play store app directly without chooser intent [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-20 09:58:18

问题


I want to open Google play store directly from my app.

Here is what I am doing now.

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

but the problem is that it shows other applications in application chooser box.

I don't want this how can I avoid Chooser box and directly open play store?

Updated :

Fixed this issue by using this:

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

but now problem is that it opens the main page of Play store but I want it to redirect to a specific app whose package name I am sending. Can any one help me with this.

Help is always appreciated.


回答1:


use this code...it works for me

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



回答2:


If i got you correctly, you are looking for this.

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 this



来源:https://stackoverflow.com/questions/27032717/how-to-open-google-play-store-app-directly-without-chooser-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!