Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

public class Main {

    public static String getBuildName(Context context) {
        PackageInfo info = getPackageInfo(context);
        if (info == null) {
            return "";
        }
        String version = info.versionName;
        if (version == null) {
            return "";
        }
        int lastIndexOfSpace = version.lastIndexOf(' ');
        if (lastIndexOfSpace != -1) {
            version = version.substring(0, lastIndexOfSpace);
        }
        String[] versionArray = version.split("[.]");
        if (versionArray.length < 4) {
            return "";
        }
        return versionArray[3];
    }

    public static PackageInfo getPackageInfo(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            return pm.getPackageInfo(context.getPackageName(), 0);
        } catch (Exception e) {
            return null;
        }
    }
}