install Apk and run Chmod command - Android Android OS

Android examples for Android OS:Shell Command

Description

install Apk and run Chmod command

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

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

public class Main {

    public static void installApkChmod(Context context, String apkPath)
            throws IOException, InterruptedException {
        if ("".equals(apkPath) || apkPath == null) {
            return;
        }//from  w w  w . ja  v  a 2 s .  c om
        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