判断进程是否存在,是否被杀死了

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iblue007/article/details/72301817

1.首先我们doc的界面截图来说明下

你可以adb进去用ps命令看看,ps后的结果是怎样的




代码如下:

public boolean isProcessExisting(String daemonName) {
        List<String> allp = getAllProcess("ps");
        String myUid = "";
        List<String> daemons = new ArrayList<String>();
        for (String line : allp) {
            if (line.contains("你要查询的进程名称")) {
                myUid = line.split("\\s+")[0];
            }
            if (line.contains(daemonName)) {
                daemons.add(line);
            }
        }

        for (int i = 0; i < daemons.size(); i++) {
            if (daemons.get(i).contains(myUid)) {
                return true;
            }
        }

        return false;
    }

    private List<String> getAllProcess(String cmd) {
        List<String> orgProcList = new ArrayList<String>();
        List<String> errMsg = new ArrayList<String>();
        Process proc = null;
        BufferedReader bufferedReader = null;
        BufferedReader errReader = null;
        try {
            proc = Runtime.getRuntime().exec(cmd);
            bufferedReader = new BufferedReader(new InputStreamReader(
                    proc.getInputStream())); // 将捕获内容转换为BufferedReader
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                orgProcList.add(str);
            }
            errReader = new BufferedReader(new InputStreamReader(
                    proc.getErrorStream()));
            str = null;
            while ((str = errReader.readLine()) != null) {
                errMsg.add(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (proc != null) {
                    proc.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (bufferedReader != null)
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (errReader != null) {
                try {
                    errReader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (orgProcList.size() != 0)
            return orgProcList;
        return errMsg;
    }


展开阅读全文

WinCE怎么判断进程是否已经存在???

12-08

在网上找到这端代码rn[code=C#]rnusing System;rnusing System.Collections.Generic;rnusing System.Text;rnusing System.Runtime.InteropServices;rnrnnamespace WindowsApplication1rnrn /// rn /// MobileUtilsrn /// rn public class MobileUtilsrn rnrn /// rn /// 判断某个进程是否已经存在rn /// rn /// rn /// rn public static bool IsProcessRunning(string processName)rn rn int hMutex;rn hMutex = CreateMutex(null, false, processName);rnrn if (GetLastError() == ERROR_ALREADY_EXISTS)rn rn ReleaseMutex(hMutex);rn return true;rn rn return false;rn rnrn /// rn /// GetLastErrorrn /// rn /// rn [DllImport("coredll.Dll")]rn private static extern int GetLastError();rnrn /// rn /// ReleaseMutexrn /// rn /// rn /// rn [DllImport("coredll.Dll")]rn private static extern int ReleaseMutex(int hMutex);rnrn /// rn /// CreateMutexrn /// rn /// rn /// rn /// rn /// rn [DllImport("coredll.Dll")]rn private static extern int CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, string lpName);rnrn /// rn /// rn /// rn private const int ERROR_ALREADY_EXISTS = 0183;rnrn /// rn /// SECURITY_ATTRIBUTESrn /// rn [StructLayout(LayoutKind.Sequential)]rn private class SECURITY_ATTRIBUTESrn rnrn /// rn /// rn /// rn public int nLength;rnrn /// rn /// rn /// rn public int lpSecurityDescriptor;rnrn /// rn /// rn /// rn public int bInheritHandle;rnrn rn rnrn[/code]rnrn我按照这个做的,我的工程名字是AProject,可是检测时候时候即使我已经开启了这个软件,再次开启时还是检测不到开启,请问CreateMutex(null, false, processName);中得软件名字该写什么呢? 论坛

没有更多推荐了,返回首页