如何以编程方式检查 Android 中是否安装了应用程序?

How to check programmatically if an application is installed or not in Android?(如何以编程方式检查 Android 中是否安装了应用程序?)

本文介绍了如何以编程方式检查 Android 中是否安装了应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们以编程方式安装了应用程序.

We have installed applications programmatically.

  1. 如果应用程序已安装在设备中,则应用程序会自动打开.
  2. 否则安装特定的应用程序.

引导我.我不知道.谢谢.

Guide Me. I have no idea. Thanks.

推荐答案

试试这个:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Add respective layout
        setContentView(R.layout.main_activity);

        // Use package name which we want to check
        boolean isAppInstalled = appInstalledOrNot("com.check.application");  
        
        if(isAppInstalled) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.check.application");
            startActivity(LaunchIntent);
                    
            Log.i("SampleLog", "Application is already installed.");          
        } else {
            // Do whatever we want to do if application not installed
            // For example, Redirect to play store

            Log.i("SampleLog", "Application is not currently installed.");
        }
    }

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

        return false;
    }

}

这篇关于如何以编程方式检查 Android 中是否安装了应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何以编程方式检查 Android 中是否安装了应用程序?

基础教程推荐