install Apk and Chmod - Android android.content.pm

Android examples for android.content.pm:Apk Install

Description

install Apk and Chmod

Demo Code

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class Main {

  public static void installApkChmod(Context context, String apkPath) throws IOException, InterruptedException {
    if ("".equals(apkPath) || apkPath == null) {
      return;//  w w w.  j a v a 2s. co m
    }
    if (apkPath.contains(context.getCacheDir() + "/")) {
      if (chmod("755", apkPath)) {
        installApk(context, apkPath);
      } else {
        installApk(context, apkPath);
      }
    } else {
      installApk(context, apkPath);
    }

  }

  public static boolean chmod(String permission, String path) throws IOException, InterruptedException {
    String command = "chmod " + permission + " " + path;
    Runtime runtime = Runtime.getRuntime();
    Process p = runtime.exec(command);
    int status = p.waitFor();
    // chmod succeed
    // chmod failed
    return status == 0;
  }

  public static void installApk(Context context, String apkPath) {
    Uri uri = Uri.fromFile(new File(apkPath));
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    context.startActivity(intent);
  }

}

Related Tutorials