Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.ComponentName;
import android.content.Context;

import android.content.pm.PackageInfo;

public class Main {
    /**
     * Fetch the versionCode from the AndroicManifest.xml
     * 
     * @param context
     * @return the versionCode
     */
    public static int getVersionCode(Context context) {
        try {
            ComponentName comp = new ComponentName(context, context.getClass());
            PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
            return pinfo.versionCode;
        } catch (android.content.pm.PackageManager.NameNotFoundException e) {
            return 0;
        }
    }
}