Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import android.text.TextUtils;

public class Main {

    public static boolean isApplicationInstalled(final Context context, final String pkgName) {
        if (TextUtils.isEmpty(pkgName)) {
            return false;
        }
        if (null == context) {
            throw new IllegalArgumentException("context may not be null.");
        }
        try {
            context.getPackageManager().getPackageInfo(pkgName, 0);
            return true;
        } catch (final NameNotFoundException e) {
            // Application not installed.
        }
        return false;
    }

    private static PackageInfo getPackageInfo(Context context, String pkgName) {
        PackageInfo packageInfo = null;
        try {
            PackageManager pm = context.getPackageManager();
            packageInfo = pm.getPackageInfo(pkgName, 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            packageInfo = new PackageInfo();
            packageInfo.versionCode = 1;
            packageInfo.versionName = "1.0";
        }
        return packageInfo;
    }
}