如果已安装或重定向到Google Play,则启动应用程序

时间:2014-08-12 14:03:18

标签: android android-intent

到目前为止,我使用此代码启动第三方应用程序(如果已安装)或重定向到Google Play以便下载(如果尚未安装):

Intent intent = Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.address");
if (intent != null)
{
/* we found the activity now start the activity */
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
/* bring user to the market or let them choose an app? */
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id="+"com.package.address"));
startActivity(intent);
}

在这里阅读我找到了启动特定活动的方式(这是我想要的结果):

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new  ComponentName("com.package.address","com.package.address.xxxxxActivity"));
startActivity(intent);

它可以启动特定的活动,但由于我非常非常有限的知识,如果没有安装该应用,我不知道如何修改条件以启动Google Play链接。

希望有人可以帮助我^^ 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用try catch查看包名是否存在

try {
    PackageManager pm=getPackageManager();
    PackageInfo info=pm.getPackageInfo("com.package.address",PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
    //launch play store
}

答案 1 :(得分:0)

完整的工作解决方案

Intent intent = getPackageManager().getLaunchIntentForPackage("slidy.whatsappsticker");
            if (intent != null)
            {
                /* we found the activity now start the activity */
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                showInterstitial();
            }
            else {
                try {
                    PackageManager pm = getPackageManager();
                    PackageInfo info = pm.getPackageInfo("com.package.name", PackageManager.GET_META_DATA);
                } catch (PackageManager.NameNotFoundException e) {
                    //launch play store
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.package.name")));
                }
            }