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 java.io.File;

import android.content.Context;

import android.content.pm.ApplicationInfo;

import android.content.pm.PackageManager;

public class Main {
    /**
     * Get give package apk file size.
     * 
     * @param context Object of {@link Context}.
     * @param pkg Package name.
     * @return Apk file size by bytes.
     */
    public final static long getAppSize(Context context, String pkg) {
        if (null == context || null == pkg) {
            return 0;
        }

        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo info = pm.getApplicationInfo(pkg, 0);
            File file = new File(info.sourceDir);
            return file.length();

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}