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;

public class Main {

    public static boolean hasNewVersion(Context context, String pkgName, int versionCodeNew) {
        if (null == context) {
            throw new IllegalArgumentException("context may not be null.");
        }
        try {
            int versionCode = getVersionCode(context, pkgName);
            if (versionCode < versionCodeNew) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public static int getVersionCode(Context context) {
        return getVersionCode(context, context.getPackageName());
    }

    public static int getVersionCode(Context context, String pkgName) {
        return getPackageInfo(context, pkgName).versionCode;
    }

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