android性能2-电量、流量、FPS获取

本文介绍了一种通过读取系统文件来获取设备电池电流值的方法,并提供了获取网络流量及帧率的功能实现。适用于多种型号的Android设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

电量

private static final String BUILD_MODEL = Build.MODEL.toLowerCase(Locale.ENGLISH);
    private static final String I_MBAT = "I_MBAT: ";
    private static final String CURRENT_NOW = "/sys/class/power_supply/battery/current_now";
    private static final String BATT_CURRENT = "/sys/class/power_supply/battery/batt_current";
    private static final String SMEM_TEXT = "/sys/class/power_supply/battery/smem_text";
    private static final String BATT_CURRENT_ADC = "/sys/class/power_supply/battery/batt_current_adc";
    private static final String CURRENT_AVG = "/sys/class/power_supply/battery/current_avg";
        /**
     * read system file to get current value
     * 
     * @return current value
     */
    public Long getCurrentValue() {
        File f = null;
        Log.d(LOG_TAG, BUILD_MODEL);
        // galaxy s4,oppo find,samgsung note2
        if (BUILD_MODEL.contains("sgh-i337") || BUILD_MODEL.contains("gt-i9505") || BUILD_MODEL.contains("sch-i545")
                || BUILD_MODEL.contains("find 5") || BUILD_MODEL.contains("sgh-m919") || BUILD_MODEL.contains("sgh-i537")
                || BUILD_MODEL.contains("x907") || BUILD_MODEL.contains("gt-n7100")) {
            f = new File(CURRENT_NOW);
            if (f.exists()) {
                return getCurrentValue(f, false);
            }
        }

        // samsung galaxy
        if (BUILD_MODEL.contains("gt-p31") || BUILD_MODEL.contains("gt-p51")) {
            f = new File(CURRENT_AVG);
            if (f.exists()) {
                return getCurrentValue(f, false);
            }
        }

        // htc desire hd ,desire z
        if (BUILD_MODEL.contains("desire hd") || BUILD_MODEL.contains("desire z")) {
            f = new File(BATT_CURRENT);
            if (f.exists())
                return getCurrentValue(f, false);
        }

        // htc sensation z710e
        f = new File(BATT_CURRENT);
        if (f.exists())
            return getCurrentValue(f, false);

        // htc one V
        f = new File(SMEM_TEXT);
        if (f.exists())
            return getSMemValue();

        // nexus one,meizu
        f = new File(CURRENT_NOW);
        if (f.exists())
            return getCurrentValue(f, true);

        // galaxy note, galaxy s2
        f = new File(BATT_CURRENT_ADC);
        if (f.exists())
            return getCurrentValue(f, false);

        // acer V360
        f = new File("/sys/class/power_supply/battery/BatteryAverageCurrent");
        if (f.exists())
            return getCurrentValue(f, false);

        // moto milestone,moto mb526
        f = new File("/sys/devices/platform/cpcap_battery/power_supply/usb/current_now");
        if (f.exists())
            return getCurrentValue(f, false);

        return null;
    }

    /**
     * get current value from smem_text
     * 
     * @return current value
     */
    public Long getSMemValue() {
        boolean success = false;
        String text = null;
        Long value = null;
        try {
            FileReader fr = new FileReader(SMEM_TEXT);
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while (line != null) {
                if (line.contains(I_MBAT)) {
                    text = line.substring(line.indexOf(I_MBAT) + 8);
                    success = true;
                    break;
                }
                line = br.readLine();
            }
            fr.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (success) {
            try {
                value = Long.parseLong(text);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
                value = null;
            }
        }
        return value;
    }

    /**
     * read system file to get current value
     * 
     * @param file
     * @param convertToMillis 
     * @return current value
     */
    public Long getCurrentValue(File file, boolean convertToMillis) {
        Log.d(LOG_TAG, "*** getCurrentValue ***");
        Log.d(LOG_TAG, "*** " + convertToMillis + " ***");
        String line = null;
        Long value = null;
        FileInputStream fs = null;
        DataInputStream ds = null;
        try {
            fs = new FileInputStream(file);
            ds = new DataInputStream(fs);
            line = ds.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fs.close();
                ds.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        if (line != null) {
            try {
                value = Long.parseLong(line);
            } catch (NumberFormatException nfe) {
                value = null;
            }
            if (convertToMillis)
                value = value / 1000;
        }
        return value;
    }
}

流量

public long getTrafficInfo(String uid) {
        long rcvTraffic = UNSUPPORTED;
        long sndTraffic = UNSUPPORTED;

        // Use getUidRxBytes and getUidTxBytes to get network traffic,these API
        // return both tcp and udp usage
        rcvTraffic = TrafficStats.getUidRxBytes(Integer.parseInt(uid));
        sndTraffic = TrafficStats.getUidTxBytes(Integer.parseInt(uid));

        if (rcvTraffic == UNSUPPORTED || sndTraffic == UNSUPPORTED) {
            return UNSUPPORTED;
        } else
            return rcvTraffic + sndTraffic;
    }

FPS

/**
     * get frame per second
     * 
     * @return frame per second
     */
public static float fps() {
        if (true) {
            long nowTime = System.nanoTime();
            float f = (float) (nowTime - startTime) / 1000000.0F;
            startTime = nowTime;
            int nowFrameNum = getFrameNum();
            final float fps = Math.round((nowFrameNum - lastFrameNum) * 1000
                    / f);
            lastFrameNum = nowFrameNum;
            return fps;
        } else {
            return -1;
        }

    }

    /**
     * get frame value
     * 
     * @return frame value
     */
public static final int getFrameNum() {
        try {
            if (process == null) {
                process = Runtime.getRuntime().exec("su");
                os = new DataOutputStream(process.getOutputStream());
                ir = new BufferedReader(new InputStreamReader(
                        process.getInputStream()));
            }
            os.writeBytes("service call SurfaceFlinger 1013" + "\n");
            os.flush();
            String str1 = ir.readLine();
            if (str1 != null) {
                int start = str1.indexOf("(");
                int end = str1.indexOf("  ");
                if ((start != -1) & (end > start)) {
                    String str2 = str1.substring(start + 1, end);
                    return Integer.parseInt((String) str2, 16);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        ok = false;
        return -1;
    }
不多说废话,看题目,本教程适合练手,会python+android基础的人群,文件较大,上传乃是下载链接,下面上目录: 1-1 课程导学 2-1 如何学好Android App性能测试? 2-10 详解【电量】监控值的获取方法 \' D, l" p) d6 d. K9 [7 p 2-11 详解【电量】监控脚本实现和数据分析 O, e4 X& K0 S% h8 v8 V9 ? 2-12 详解【内存】监控值的获取方法 k! e6 e# C" K% z9 k- l 2-13 详解【内存】监控脚本实现和数据分析0 d; e- S% G6 r3 H: g 2-14 详解【FPS&过度渲染】的概念和监控方法 - 分析页面卡慢的方法# G! _2 O9 T* j" K s3 v6 C0 l 2-2 工欲善其事必先利其器-性能测试环境准备 2-3 详解【启动时间】监控值的获取方法0 n( p* l; g C 2-4 详解【启动时间】监控脚本实现% B2 z( C( E& S: n r1 e 2-5 详解【启动时间】数据分析 2-6 【启动时间】时间戳差值监控方法概要介绍 2-7 详解【CPU】监控值的获取方法、脚本实现和数据分析 2-8 详解【流量】监控值的获取方法7 r7 ~/ D5 |+ h9 m9 i6 p) b: Y 2-9 详解【流量】监控脚本实现和数据分析2 [9 {# {$ c9 k/ T, `/ t" \ 3-1 为什么需要使用框架实现自动化测试? 3-10 UnitTest框架之TestCase,TestSuite,TestRunner简介3 A2 {1 F2 @; K 3-11 UnitTest框架之TestSuite,TestRunner自动化测试 3-12 数据驱动框架DDT简介 3-13 数据驱动框架DDT的使用方法 3-14 数据驱动框架DDT实战; 3-2 准备一个被测APP 3-3 工欲善其事必先利其器-自动化测试环境准备 3-4 Android App自动化测试(一) 3-5 Android App自动化测试(二) 3-6 Android App自动化测试(三) 3-7 UnitTest框架之TestFixture简介 3-8 UnitTest框架之TestFixture自动化测试(一) 3-9 UnitTest框架之TestFixture自动化测试(二) 4-1 如何学好Android App API接口测试? 4-10 Fiddler构造HTTP Get请求 4-11 Fiddler构造HTTP Post请求 4-12 Fiddler抓取手机上的网络数据包 4-13 为什么使用PostMan做API接口测试 4-14 工欲善其事必先利其器-PostMan工具准备 4-15 PostMan测试HTTP Get请求 4-16 PostMan测试HTTP Post请求 4-17 数据驱动DDT实现API接口自动化测试简介) 4-18 Python requests测试HTTP中的Get、Post请求 4-19 数据驱动DDT实现API接口自动化测试(一) 4-2 什么是API 4-20 数据驱动DDT实现API接口自动化测试(二); 4-3 抓包神器Fiddler简介 4-4 Fiddler抓包原理解析 4-5 Fiddler修改客户端发出的请求(一) 4-6 Fiddler修改客户端发出的请求(二) 4-7 Fiddler修改服务器端返回的内容 4-8 Fiddler实现会话的过滤、对比及请求的编解码 4-9 Fiddler实现Host的配置 5-1 测试工程师为什么需要掌握持续集成? 5-2 持续集成的概念、流程和意义 5-3 讲解持续集成工具Jenkins3 5-4 Jenkins工具密码的修改 5-5 Jenkins工具的配置说明 5-6 Jenkins工具系统配置和Job配置 5-7 Jenkins工具手动持续集成实战 5-8 Jenkins工具自动化持续集成实战 6-1 Native App自动化测试及Appuim框架介绍 6-2 自动化测试环境、元素识别工具、脚本设计原则-LOVE原则的讲解 6-3 Native App自动化脚本的实现 6-4 Appium自动化测试框架API讲解与案例实践(一) 6-5 Appium自动化测试框架API讲解与案例实践(二) 6-6 Appium自动化测试框架API讲解与案例实践(三) 6-7 Appium自动化测试框架API讲解与案例实践(四) 6-8 Appium自动化测试框架API讲解与案例实践(五) 6-9 Appium自动化测试框架API讲解与案例实践(六) 7-1 Hybrid App自动化测试概要 7-2 Appium基于Sele
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值