获取Android所有APP的流量信息

本文深入解析了一种用于获取应用程序流量排名的算法实现。通过遍历已安装应用,检查其是否请求了互联网权限,进而获取每个应用的接收和发送流量数据,并将其转换为可读格式,最终形成流量排名列表。文章涵盖了从权限检查到流量统计的全过程。

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

fun getFlowRanking(): ArrayList<FlowRankingData> {
    var installedPackages =
        context.packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES or PackageManager.GET_PERMISSIONS)
    var listInfo = ArrayList<FlowRankingData>()

    for (packagesInt in installedPackages) {
        val permissionsArray = packagesInt.requestedPermissions

        if (permissionsArray != null && permissionsArray.size > 0) {

            for (array in permissionsArray) {
                if (array.equals("android.permission.INTERNET")) {
                    val loadLabel =
                        packagesInt.applicationInfo.loadLabel(context.packageManager)
                            .toString()
                    val uidRxBytes = TrafficStats.getUidRxBytes(packagesInt.applicationInfo.uid)

                    if (!(uidRxBytes != 0L && !(TrafficStats.getUidRxBytes(packagesInt.applicationInfo.uid) == -1L && TrafficStats.getUidTxBytes(
                            packagesInt.applicationInfo.uid
                        ) == -1L))
                    ) {
                        val uidData = getUidData(packagesInt.applicationInfo.uid)
                        if (uidData != null) {
                            val textReceived = TraRankingData().textReceived
                            val textSent = TraRankingData().textSent
                            if (textReceived != null && textSent != null) {
                                listInfo.add(
                                    FlowRankingData(
                                        loadLabel,
                                        bytegetMb(textReceived),
                                        bytegetMb(textSent)
                                    )
                                )
                            }else{
                                listInfo.add(
                                    FlowRankingData(
                                        loadLabel,
                                        bytegetMb(0),
                                        bytegetMb(0)
                                    )
                                )
                            }
                        }
                    }
                }
            }
        }

    }
    return listInfo
}

fun getUidData(localUid: Int): TraRankingData? {

    val dir = File("/proc/uid_stat/")
    val children = dir.list()
    val stringBuffer = StringBuffer()
    var traRankingData: TraRankingData? = TraRankingData()
    if (children != null) {
        for (i in children.indices) {
            stringBuffer.append(children[i])
            stringBuffer.append("   ")
        }
        if (!Arrays.asList(*children).contains(localUid.toString())) {
            return null
        }
    }
    val uidFile = File("/proc/uid_stat/" + localUid.toString())
    val uidActualFileReceived = File(uidFile, "tcp_rcv")
    val uidActualFileSent = File(uidFile, "tcp_snd")
    var textReceived: String? = "0"
    var textSent: String? = "0"
    try {
        val brReceived =
            BufferedReader(FileReader(uidActualFileReceived))
        val brSent =
            BufferedReader(FileReader(uidActualFileSent))
        var receivedLine: String?
        var sentLine: String?
        if (brReceived.readLine().also { receivedLine = it } != null) {
            textReceived = receivedLine
        }
        if (brSent.readLine().also { sentLine = it } != null) {
            textSent = sentLine
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }

    if (traRankingData != null) {
        traRankingData.textReceived = textReceived?.toLong()
        traRankingData.textSent = textSent?.toLong()
    } else {
        var traRankingData = TraRankingData()
        traRankingData.textReceived = textReceived?.toLong()
        traRankingData.textSent = textSent?.toLong()
    }
    return traRankingData
}


fun bytegetMb(byte: Long): String {
    val kb: Long = 1024
    val mb = kb * 1024
    val gb = mb * 1024
    return if (byte >= gb) {
        String.format("%.1f GB", byte as Float / gb)
    } else if (byte > kb) {
        val f = byte as Float / kb
        String.format(if (f > 100) "%.0f KB" else "%.1f KB", f)
    } else if (byte >= mb) {
        val f = byte as Float / mb
        String.format(if (f > 100) "%.0f MB" else "%.1f MB", f)
    } else {
        String.format("%d B", byte)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值