force App to Stop by package name - Android App

Android examples for App:Package

Description

force App to Stop by package name

Demo Code


//package com.java2s;
import android.os.Build;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

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

    public static void forceStop(String pkgName) {
        exec(true, true, "am force-stop " + pkgName, new StringBuilder(),
                new StringBuilder());
    }//  ww w . java 2 s . co m

    public static void forceStop(String pkgName, int pid) {
        if (Build.VERSION.SDK_INT < 14) {
            exec(true, true, "kill -9 " + pid);
        } else {
            forceStop(pkgName);
        }
    }

    public static void forceStop(String[] pkgNames, Integer[] pids) {
        if (Build.VERSION.SDK_INT < 14) {
            String[] cmds = new String[pids.length];
            for (int i = 0; i < pids.length; i++) {
                cmds[i] = "kill -9 " + pids[i];
            }
            exec(true, true, cmds);
        } else {
            String[] cmds = new String[pkgNames.length];
            for (int i = 0; i < pkgNames.length; i++) {
                cmds[i] = "am force-stop " + pkgNames[i];
            }
            exec(true, true, cmds);
        }
    }

    public static int exec(boolean root, boolean waitFor, String command) {
        return exec(root, waitFor, command, null, null);
    }

    public static int exec(boolean root, boolean waitFor, String[] commands) {
        return exec(root, waitFor, commands, null, null);
    }

    public static int exec(boolean root, boolean waitFor, String command,
            StringBuilder out, StringBuilder err) {
        return exec(root, waitFor, new String[] { command }, out, err);
    }

    public static int exec(boolean root, boolean waitFor,
            String[] commands, StringBuilder out, StringBuilder err) {
        Log.d(TAG, "exec root: " + root + ", waitFor: " + waitFor
                + ", command = " + Arrays.toString(commands));
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        int exitValue = -1;
        if (commands == null) {
            return -1;
        }

        try {
            if (root) {
                process = runtime.exec("su\n");//????root?????
                for (String command : commands) {
                    process.getOutputStream().write(
                            (command + "\n").getBytes());//????
                    process.getOutputStream().flush();
                }
                process.getOutputStream().write("exit\n".getBytes());//??su
            } else {
                process = runtime.exec(commands);
            }

            process.getOutputStream().write("exit\n".getBytes()); //??process
            if (waitFor) {
                process.waitFor();//??process?????
            }

            byte[] outData = getData(process.getInputStream());//????????
            if (out != null) {
                out.append(new String(outData));
            }

            byte[] errorData = getData(process.getErrorStream());//????????
            if (err != null) {
                err.append(new String(errorData));
            }

            while (true) {
                try {
                    exitValue = process.exitValue();
                    break;
                } catch (Exception e) {
                    // TODO: handle exception
                    Thread.sleep(100);
                    Log.d(TAG, "exec wait to get exitValue!");
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            Log.e(TAG, e.toString());
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
        Log.d(TAG, "exitValue: " + exitValue + ", out: "
                + (out == null ? "" : out.toString()) + ", err: "
                + (err == null ? "" : err.toString()));
        return exitValue;
    }

    private static byte[] getData(InputStream is) throws IOException {
        DataInputStream dis = new DataInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read = -1;
        byte[] buffer = new byte[1024];
        while ((read = dis.read(buffer)) > 0) {
            baos.write(buffer, 0, read);
            Arrays.fill(buffer, 0, buffer.length, (byte) 0);
        }
        baos.close();
        return baos.toByteArray();
    }
}

Related Tutorials