• 【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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    执行效率对比

    对比下四种方式:
    在这里插入图片描述
    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();
         }
    
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

  • 相关阅读:
    <计算机网络自顶向下>
    【构建并发程序】7-如何理解并发队列?
    LeetCode24.两两交换链表中的节点
    LeetCode知识点总结 - 376
    rust换源
    转运RNA(tRNA)甲基化修饰7-甲基胞嘧啶(m7C)|tRNA-m7G
    基于linux开发的实现类似unix的文件系统
    解决AnyViewer干扰控制端输入法的问题
    Http代理与socks5代理有何区别?如何选择?(一)
    81 · 寻找数据流的中位数
  • 原文地址:https://blog.csdn.net/qq_23049111/article/details/126097443