Android根据包名判断程序是否在运行,是否位于最前端,以及将应用置顶到最前端

1.判断程序是否在运行

 public static boolean isRunningApk(String packagename) {
        Set<String> rProcess = new HashSet<String>();
        try {
          Process p = Runtime.getRuntime().exec("ps");//使用ps命令查找正在运行的进程
            BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            int index = 0;
            while ((line = bufferedReader.readLine()) != null) {
                if (index > 0) {
                    StringTokenizer  stringTokenizer = new StringTokenizer(line);
                    int length = stringTokenizer.countTokens();
                    if (length > 0) {
                        String uid = stringTokenizer.nextToken();
                        if (uid.startsWith("u0_")) {
                            String processInfo = "";
                            for (int i = 0; i < (length - 1); i++) {
                                if (!(i == (length - 2))) {
                                    st.nextToken();
                                } else {
                                    processInfo = st.nextToken();
                                }
                            }
                            if (!TextUtils.isEmpty(processInfo)) {
                                if (processInfo.contains(":")) {
                                    String a[] = processInfo.split(":");
                                    rProcess.add(a[0]);
                                } else {
                                    rProcess.add(processInfo);
                                }
                            }
                        }
                    }
                }
                index++;
            }
        } catch (IOException e) {
            loggerUtil.Error(TAG, "getRunningApk err=" + e.toString());
        }
        for (String pro : rProcess) {
            if (pro.equals(packagename))
                return true;
        }
        return false;
    }

2.判断程序是否位于最前端

/**
     * 判断本应用是否已经位于最前端
     *
     * @param context
     * @return 本应用已经位于最前端时,返回 true;否则返回 false
     */
    public static boolean isRunningForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfoList = activityManager.getRunningAppProcesses();
        //枚举进程
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfoList) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
                    return true;
                }
            }
        }
        return false;
    }

3.将应用程序置顶到最前端

 /**
     * 将本应用置顶到最前端
     * 当本应用位于后台时,则将它切换到最前端
     *
     * @param context
     */
    public static void setTopApp(Context context) {
        if (!isRunningForeground(context)) {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            //获得当前运行的task(任务)
            List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(100);
            for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                //找到本应用的 task,并将它切换到前台
                if (taskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
                    activityManager.moveTaskToFront(taskInfo.id, 0);
                    break;
                }
            }
        }
    }

根据包名判断程序是否在运行上面那个好像失效了,修改之后可以:
下面展示一些 内联代码片

/**
     * 判断包名是否在运行
     * 使用ps命令,获取当前运行程序的包名
     *
     * @param packagename
     * @return
     */
    public static boolean isRunningApk(String packagename) {
        Set<String> rProcess = new HashSet<String>();
        String cmd = "ps";
        String result = "";
        DataOutputStream dos = null;
        DataInputStream dis = null;

        try {
            Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
            dos = new DataOutputStream(p.getOutputStream());
            dis = new DataInputStream(p.getInputStream());

            Log.i(TAG, cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            String line = null;
            int index = 0;
            while ((line = dis.readLine()) != null) {
                if (index > 0) {
                    StringTokenizer st = new StringTokenizer(line);
                    int length = st.countTokens();
                    if (length > 0) {
                        String uid = st.nextToken();//0 index USER
                        if (uid.startsWith("u0_")) {
                            String processInfo = "";
                            for (int i = 0; i < (length - 1); i++) {
                                if (!(i == (length - 2))) {
                                    st.nextToken();
                                } else {
                                    processInfo = st.nextToken();
                                }
                            }
                            if (!TextUtils.isEmpty(processInfo)) {
                                if (processInfo.contains(":")) {
                                    String a[] = processInfo.split(":");
                                    rProcess.add(a[0]);
                                } else {
                                    rProcess.add(processInfo);
                                }
                            }
                        }
                    }
                }
                index++;
            }
        } catch (IOException e) {
            e.printStackTrace();
            loggerUtil.Error(TAG, "isRunning Exception:" + e.getMessage());


        }
        for (String pro : rProcess) {
            if (pro.equals(packagename))
                return true;
        }
        return false;
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值