简体   繁体   English

如果未安装应用程序,如何检查是否已从我的应用程序安装了应用程序?

[英]how to check whether an app is installed from my app if the app is not installed go to play store?

I have an application... on button click in my application i need to check whether a particular app is installed in the phone or not..If the application is not installed it should redirect to play store. 我有一个应用程序...在我的应用程序中单击按钮时,我需要检查手机中是否安装了特定应用程序。如果未安装该应用程序,则应重定向到播放商店。

How can i do that please help? 我该怎么办?

public class Example extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Put the package name here...
        boolean installed  =   appInstalledOrNot("com.Ch.Example.pack");  
        if(installed) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.Ch.Example.pack");
            startActivity(LaunchIntent);

            System.out.println("App already installed on your phone");        
        }
        else {
            System.out.println("App is not installed on your phone");
        }
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed ;
    }
}

Does the above example work for checking whether an app is installed or not my my application. 上面的示例是否可用于检查是否已安装我的应用程序。

To check whether an app is installed in the device use following method and params GooglePlayStorePackageNameOld & GooglePlayStorePackageNameNew both holds the package names of apps 要检查设备中是否安装了应用,请使用以下方法和参数GooglePlayStorePackageNameOldGooglePlayStorePackageNameNew都保存应用的程序包名称

private static final String GooglePlayStorePackageNameOld = "com.google.market";
private static final String GooglePlayStorePackageNameNew = "com.android.vending";

use and modify the following method accordingly to check if the app is presently installed in the device or not 相应地使用和修改以下方法,以检查应用程序当前是否已安装在设备中

public boolean isAppInstalled() {
            PackageManager packageManager = getApplication().getPackageManager();
            List<PackageInfo> packages = packageManager
                    .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
            for (PackageInfo packageInfo : packages) {
                if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld)
                        || packageInfo.packageName
                                .equals(GooglePlayStorePackageNameNew)) {
                    googlePlayStoreInstalled = true;
                    break;
                } else {
                    googlePlayStoreInstalled = false;
                }
            }
            return googlePlayStoreInstalled;
        }

and to navigate user to a particular Play Store link use the following code 并将用户导航到特定的Play商店链接,请使用以下代码

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("http://play.google.com/store/apps/details?id=" + appPackageName)));
}

EDIT According to your code 编辑根据您的代码

Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.Ch.Example.pack");
            startActivity(LaunchIntent);

will launch the app which has the package name com.Ch.Example.pack 将启动包名称为com.Ch.Example.pack的应用程序

The way you check if the app is installed is a standard way. 您检查应用程序是否已安装的方式是一种标准方式。 To redirect to playStore you can use the following snippet 要重定向到playStore,您可以使用以下代码段

private goToPlayStore(String packageName) {
  Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName))
  startActivity(intent);
}

also with respect to the comment, you should check first if the market is installed, the package name should be com.android.vending . 同样对于注释,您应该首先检查是否安装了市场,包名称应该为com.android.vending This code will throw an exception if market is not available. 如果没有市场,此代码将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM