Launching an application if installed or redirect to Google Play

↘锁芯ラ 提交于 2019-12-13 23:34:30

问题


Until now I was using this code to launch a 3rd party application if installed or redirect to Google Play to download it if not installed already:

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

Reading here I found the way to launch a particular activity instead (that was my desired result):

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

It works launching the particular activity, but with my very very very limited knowledge I don't know how to modify the condition to launch the link to Google Play if the app isn't installed.

Hope someone can help me^^ Thanks!


回答1:


you can use a try catch to see if the package name exists

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



回答2:


A complete working solution

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


来源:https://stackoverflow.com/questions/25266336/launching-an-application-if-installed-or-redirect-to-google-play

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