杀死Android中的其他进程 [英] Kill other process in Android

查看:121
本文介绍了杀死Android中的其他进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过编程方式终止或终止正在运行的其他进程吗?我被问到我的朋友,并告诉我应该使用信息亭模式!!

Can I end or kill other process running, programmatically? I'm asked my friend and tell me should using something like kiosk mode !!

有人可以帮我解决这个问题吗?.

Could anyone help me to solve this problem ..

所有的问候和感谢.

推荐答案

由于Android 2.2(或更高版本,我不记得了),您只能使用意图杀死自己的应用.要从您的应用中终止其他进程,您必须具有 rooted 设备,手动解析ps命令输出,获取所需的进程ID,执行su,将kill命令写入此管道,现在我们开始.我为自己的库编写了此代码,因此这些方法是分开的,但是希望您能理解:

Since Android 2.2 (or higher, I don't remember), you can only kill your own app using an intent. To kill another processes from your app you must have rooted device, parse the ps command output manually, get needed process ids, exec the su, write the kill command to this pipe, and here we go. I wrote this code for my own library, so this methods are separated, but hope you'll understand:

    public interface ExecListener {
        void onProcess (String line, int i);
    }

    public static String exec (String input) throws IOException, InterruptedException {
        return exec (input, null);
    }

    public static String exec (String input, ExecListener listener) throws IOException, InterruptedException {

        Process process = Runtime.getRuntime ().exec (input);
        BufferedReader in = new BufferedReader (new InputStreamReader (process.getInputStream ()));

        StringBuilder output = new StringBuilder ();

        int i = 0;
        String line;

        while ((line = in.readLine ()) != null) {

            if (listener == null)
               output.append (line).append ("\n");
            else
               listener.onProcess (line, i);

            ++i;

        }

        in.close ();
        process.waitFor ();

        return output.toString ().trim ();

    }

    public static void execSU (List<String> cmds) throws IOException, InterruptedException {

        Process process = Runtime.getRuntime ().exec ("su");
        DataOutputStream os = new DataOutputStream (process.getOutputStream ());

        for (String cmd : cmds) os.writeBytes (cmd + "\n");

        os.writeBytes ("exit\n");

        os.flush ();
        os.close ();

        process.waitFor ();

    }

    public static void killProcess (String id) throws IOException, InterruptedException {
        return killProcess (new String[] {id});
    }

    public static void killProcess (final String[] ids) throws IOException, InterruptedException {

        final ArrayList<String> procs = new ArrayList<> ();

        exec ("ps", new ExecListener () {

            @Override
            public void onProcess (String line, int i) {

                if (i > 0) {

                    String[] data = Arrays.explode ("\\s+", line);

                    for (String id : ids)
                      if (id.equals (data[8]))
                        procs.add (data[1]);

                }

            }

        });

        if (procs.size () > 0) {

            ArrayList<String> cmds = new ArrayList<> ();
            cmds.add ("kill -9 " + Arrays.implode (" ", procs));

            execSU (cmds);

        }

    }

这篇关于杀死Android中的其他进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆