Android How to - Open apk








Question

We would like to know how to open apk.

Answer

/*ww  w. j a  va  2s.  com*/
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
public class Main{

    public static void openApk(Context context, String packageName) {
        if(isApkAvailable(context, packageName)){
            Intent intent = new Intent();
            intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
            context.startActivity(intent);
        }else{
            Uri uri = Uri.parse("market://details?id=" + packageName);
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(it);
        }
    }

    public static boolean isApkAvailable(Context context, String packageName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(
                    packageName, 0);

        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
        }
        if(packageInfo ==null){
            return false;
        }else{
            return true;
        }
    }
}