Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.InputStream;

import java.util.Properties;

import android.content.Context;

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

import android.util.Log;

public class Main {
    private static final String TAG = "Util";

    public static String getVersionName(Context ctx) {
        String packageName = ctx.getPackageName();
        if (packageName.equals("com.MyGame.Midlet")) {
            return getVerNameFromAssert(ctx);
        } else {
            return getAppVer(ctx, packageName);
        }

    }

    private static String getVerNameFromAssert(Context context) {

        String versionName = "";
        try {
            Properties pro = new Properties();
            InputStream is = context.getAssets().open("channel.properties");
            pro.load(is);
            String tmpVersionName = pro.getProperty("versionName");

            versionName = new String(tmpVersionName.getBytes("ISO-8859-1"), "UTF-8");

            is.close();
            is = null;
        } catch (Exception e) {
            versionName = "";
            Log.e(TAG, "AppConfig.loadVersion have Exception e = " + e.getMessage());
        }
        return versionName;

    }

    private static String getAppVer(Context context, String packageName) {
        String ver = null;

        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            ver = pi.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ver;
    }
}