如何检查是否安装了应用并将其打开?

时间:2016-03-30 18:09:48

标签: android android-intent

我开发了两个应用程序。在一个我正在显示来自youtube的视频。在第二个,我显示新闻,我有一个菜单,必须打开第一个应用程序。

我知道有意图打开第二个应用,但我找不到检查应用是否安装的方法。

如何检查应用程序是否已安装,如果是,则打开应用程序,如果当前未安装,则打开应用程序的Playstore页面。

2 个答案:

答案 0 :(得分:0)

使用此方法。

public void OpenApp() {
    PackageManager pm = getPackageManager();
    final String LiveAppPackage = "com.example.app"; //Change to your package name.
    try {
        pm.getPackageInfo(LiveAppPackage, PackageManager.GET_ACTIVITIES);
        Intent intent = pm.getLaunchIntentForPackage(LiveAppPackage);
            if (intent == null) {
                throw new PackageManager.NameNotFoundException();
            }
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            context.startActivity(intent);

    }
    catch (PackageManager.NameNotFoundException e) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                ActivityThis);

        DialogInterface.OnClickListener onClickListener =  new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(which == DialogInterface.BUTTON_POSITIVE){
                                //Try to open Market and if fails, open play store.
                                try {
                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri
                                            .parse("market://details?id="
                                                    + LiveAppPackage)));
                                } catch (android.content.ActivityNotFoundException e1) {
                                    startActivity(new Intent(
                                            Intent.ACTION_VIEW,
                                            Uri.parse("https://play.google.com/store/apps/details?id="
                                                    + LiveAppPackage)));
                                }

                        } else{
                            dialog.dismiss();
                        }
                    }
                };

        alertDialogBuilder.setTitle("App Title")
                .setCancelable(true)
                .setPositiveButton("Install", onClickListener);
        alertDialogBuilder.setNegativeButton("Cancel", onClickListener);

        alertDialogBuilder.setMessage("Second app is not currently installed\n\nLike to install it?"); //Change to your message

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
}

答案 1 :(得分:0)

要知道应用程序是否已安装,您可以像这样使用PackageManager: -

private boolean isAppInstalled(String packageName) {
    PackageManager pm = getPackageManager();
    boolean isAppInstalled;
    try { 
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        isAppInstalled = true;
    } 
    catch (PackageManager.NameNotFoundException e) {
        isAppInstalled = false;
    } 
    return appInstalled;
} 

要使用此方法,您需要传递第二个应用的包名称。我想你知道packageName。休息是蛋糕。 :)

appInstalledOrNot("com.dexter.lab");