【Android】获取当前进程名的四种方法及效率对比

四种获取方式

public static String getProcessName_1() {
        try {
            File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");
            BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));
            String processName = mBufferedReader.readLine().trim();
            mBufferedReader.close();
            return processName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

public static String getProcessName_2(Context context) {
    // 获取此进程的标识符
    int pid = android.os.Process.myPid();
    // 获取活动管理器
    ActivityManager activityManager = (ActivityManager)
            context.getSystemService(Context.ACTIVITY_SERVICE);
    // 从应用程序进程列表找到当前进程,是:返回当前进程名
    for (ActivityManager.RunningAppProcessInfo appProcess :
            activityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}


public static String getProcessName_3() {
    String processName = null;
    try {
        final Method declaredMethod = Class.forName("android.app.ActivityThread", false, Application.class.getClassLoader())
                .getDeclaredMethod("currentProcessName", (Class<?>[]) new Class[0]);
        declaredMethod.setAccessible(true);
        final Object invoke = declaredMethod.invoke(null, new Object[0]);
        if (invoke instanceof String) {
            processName = (String) invoke;
        }
    } catch (Throwable e) {
    }
    return processName;
}

@RequiresApi(api = Build.VERSION_CODES.P)
private String getProcessName_4() {
    return Application.getProcessName();
}

执行效率对比

对比下四种方式:
在这里插入图片描述
1和2的耗时相差不大,1的耗时图如下
在这里插入图片描述
1的耗时主要集中在字符串的拼接、FileReader的创建和File文件的读取,这3项耗时占比80%
在这里插入图片描述
方法2的耗时集中在跨进程通信,耗时占比90%

方法4是当之无愧的冠军,但是对系统版本有要求。

最优解

融合方法3和方法4:

 public String processName(){
     
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
       return getProcessName_4();
     }else{
       return getProcessName_3();
     }

 }

参考:
看我一波,Android获取进程名函数,代码优化到极致的操作
android:process 的坑,你懂吗

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下方法获取Android当前进程CPU使用率: 1. 获取当前进程ID: ``` int pid = android.os.Process.myPid(); ``` 2. 获取当前进程CPU使用时间: ``` long cpuTime = 0; RandomAccessFile reader = null; try { reader = new RandomAccessFile("/proc/" + pid + "/stat", "r"); String line = reader.readLine(); String[] arr = line.split("\\s+"); cpuTime = Long.parseLong(arr[13]) + Long.parseLong(arr[14]); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 3. 获取系统总CPU使用时间: ``` long totalCpuTime1 = 0; long idleCpuTime1 = 0; RandomAccessFile cpuReader = null; try { cpuReader = new RandomAccessFile("/proc/stat", "r"); String cpuLine = cpuReader.readLine(); String[] cpuArr = cpuLine.split("\\s+"); for (int i = 2; i < cpuArr.length; i++) { totalCpuTime1 += Long.parseLong(cpuArr[i]); } idleCpuTime1 = Long.parseLong(cpuArr[4]); } catch (IOException e) { e.printStackTrace(); } finally { if (cpuReader != null) { try { cpuReader.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 4. 计算当前进程CPU使用率: ``` long totalCpuTime2 = 0; long idleCpuTime2 = 0; RandomAccessFile cpuReader2 = null; try { cpuReader2 = new RandomAccessFile("/proc/stat", "r"); String cpuLine2 = cpuReader2.readLine(); String[] cpuArr2 = cpuLine2.split("\\s+"); for (int i = 2; i < cpuArr2.length; i++) { totalCpuTime2 += Long.parseLong(cpuArr2[i]); } idleCpuTime2 = Long.parseLong(cpuArr2[4]); } catch (IOException e) { e.printStackTrace(); } finally { if (cpuReader2 != null) { try { cpuReader2.close(); } catch (IOException e) { e.printStackTrace(); } } } double cpuUsage = ((double) (totalCpuTime2 - totalCpuTime1) - (double) (idleCpuTime2 - idleCpuTime1)) / (double) (totalCpuTime2 - totalCpuTime1) * 100.0; ``` 这样就可以得到当前进程的CPU使用率了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值