• 【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 的坑,你懂吗

  • 相关阅读:
    八股文系列:Java的并发编程
    Streamsets Data Collector 3.12
    技术对接76
    FreeRTOS-链表的源码解析
    selenium(练习)提取dou yu网站上的数据
    〖全域运营实战白宝书 - 运营角色认知篇⑥〗- 不同企业的 “运营“ 不一样
    iOS之卡顿检测
    Redis 学习笔记
    排序算法-冒泡、选择、堆、插入、归并、快速、希尔
    【设计一个缓存--针对各种类型的缓存】
  • 原文地址:https://blog.csdn.net/qq_23049111/article/details/126097443