点击按钮打开应用程序如果安装,否则在Android中打开Play商店?

时间:2014-07-11 09:30:20

标签: android

我正在创建一个应用程序,在其中我提供了另一个应用程序的链接。如果该应用程序已经安装在用户设备上,则打开它,否则打​​开Android设备的Google Play商店页面。

2 个答案:

答案 0 :(得分:6)

用于打开应用:

Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("app package name");
if (i == null)
    throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {

//if not found in device then will come here
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setData(Uri.parse("market://details?id=com.example.android"));
  startActivity(intent);
}

答案 1 :(得分:1)

我想你应该试试这个:

PackageManager packageManager = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("file/*");
List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                                PackageManager.GET_ACTIVITIES));

if (list.size > 0) {
    // File explore is present (Size tells how many file explorers are present)
} else {

    // Not present
    // Just pointing to this app - https://play.google.com/store/apps/details?id=com.rhmsoft.fm
    // You can choose whichever you need

     try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.rhmsoft.fm")));
        } catch (android.content.ActivityNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW,     Uri.parse("http://play.google.com/store/apps/details?id=com.rhmsoft.fm")));
      }
    }