Android中TrafficStats流量监控类

这个类提供了网络流量统计,这个统计包括上传和下载的字节数和网络数据包数。需要注意的是这个统计不能在所有的平台上使用,如果设备不支持的话,就会返回UNSUPPORTED。

常用函数:

public static void setThreadStatsTag(int tag)

public static int getThreadStatsTag()

public static void clearThreadStatsTag()

public static void tagSocket(Socket socket)

public static void untagSocket(Socket socket)
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Android 4.0.4的sdk DDMS中,有了一个工具:Network Traffic Tool 。通过这个工具可以实时地监测网络的使用情况,使程序员更好的发现自己的应用程序在什么时候发送接收了多少的网络数据.

如果要更加清楚地看清每一个网络连接的使用情况可以在程序中对网络连接打tag,如对socket连接可以这样:

TrafficStats.setThreadStatsTag(0xF00D);
TrafficStats.tagSocket(outputSocket);
// Transfer data using socket
TrafficStats.untagSocket(outputSocket);
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

对于Apache HttpClient and URLConnection 会自动打上tag,所以只要设置上tag名就可以了:

对于Apache HttpClient and URLConnection 会自动打上tag,所以只要设置上tag名就可以了:

TrafficStats.setThreadStatsTag(0xF00D);
try {
  // Make network request using HttpClient.execute()
} finally {
  TrafficStats.clearThreadStatsTag();
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

其他函数:

public static void incrementOperationCount(int operationCount)

public static void incrementOperationCount(int tag, int operationCount)
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

增加网络操作的数量

public static long getMobileTxPackets()   通过流量发送的数据包数

public static long getMobileRxPackets() 通过流量接收到的数据包数

public static long getMobileTxBytes()  通过流量发送的字节数

public static long getMobileRxBytes()  通过流量接收的字节数

public static long getTotalTxPackets()  发送的数据包总数,包括流量和WIFI

public static long getTotalRxPackets()  接收的数据包总数,包括流量和WIFI

public static long getTotalTxBytes()  发送的字节总数,包括流量和WIFI

public static long getTotalRxBytes()  接收的字节总数,包括流量和WIFI

public static long getUidTxPackets(int uid) 获取指定网络UID发送的数据包数

public static long getUidRxPackets(int uid)  获取指定网络UID接收的数据包数

public static long getUidTxBytes(int uid)  获取指定网络UID发送的字节数

public static long getUidRxBytes(int uid)  获取指定网络UID接收的字节数
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

下面写一个小demo

MainActivity.Java

package com.xxx.cn.trafficstatstest;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    private List apps = new ArrayList <ApplicationBean>();
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.lv_app);
        getAppTrafficStatList();
        MyAdapter adapter = new MyAdapter(apps, this);
        mListView.setAdapter(adapter);
    }

    public void getAppTrafficStatList() {
        PackageManager pm = getPackageManager();
        //得到安装的所以应用
        List<PackageInfo> pinfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES |
                        PackageManager.GET_PERMISSIONS);
        //找出需要网络权限的应用,得到这个应用上传和下载的数据量
        for (PackageInfo info : pinfos) {
            String[] pers = info.requestedPermissions;

            if (pers != null && pers.length > 0) {
                for (String per : pers)
                    //找到需要网络权限的应用
                    if ("android.permission.INTERNET".equals(per)) {
                        int uid = info.applicationInfo.uid;
                        String lable = (String) info.applicationInfo.loadLabel(pm);

                        Drawable icon = null;
                        try {
                            icon = pm.getApplicationIcon(info.packageName);
                        } catch (PackageManager.NameNotFoundException e) {
                            e.printStackTrace();
                        }

                        long rx = TrafficStats.getUidRxBytes(uid);
                        long tx = TrafficStats.getUidTxBytes(uid);

                        ApplicationBean app = new ApplicationBean();
                        app.setName(lable);
                        app.setIcon(icon);
                        app.setRx(rx);
                        app.setTx(tx);
                        apps.add(app);
                    }
            }
        }
    }

}

 
 
  • 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
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 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
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

MyAdapter.java

package com.xxx.cn.trafficstatstest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List mApps;
    private LayoutInflater inflater;

    public MyAdapter(List apps, Context context) {
        this.mApps = apps;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mApps.size();
    }

    @Override
    public Object getItem(int position) {
        return mApps.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.app_item, null);
            view = new ViewHolder();
            view.name = (TextView) convertView.findViewById(R.id.app_name);
            view.icon = (ImageView) convertView.findViewById(R.id.app_icon);
            view.rx = (TextView) convertView.findViewById(R.id.app_rx);
            view.tx = (TextView) convertView.findViewById(R.id.app_tx);
            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }

        ApplicationBean app = (ApplicationBean) mApps.get(position);
        view.name.setText(app.getName());
        view.icon.setImageDrawable(app.getIcon());
        view.rx.setText("接收:" + app.getRx());
        view.tx.setText("上传:" + app.getTx());

        return convertView;
    }


    public static class ViewHolder {
        public TextView name;
        public ImageView icon;
        public TextView rx;
        public TextView tx;
    }
}

 
 
  • 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
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 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
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

ApplicationBean.java

package com.xxx.cn.trafficstatstest;

import android.graphics.drawable.Drawable;

public class ApplicationBean {
    private String name;
    private Drawable icon;
    private long rx;
    private long tx;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Drawable getIcon() {
        return icon;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }

    public long getTx() {
        return tx;
    }

    public void setTx(long tx) {
        this.tx = tx;
    }

    public long getRx() {
        return rx;
    }

    public void setRx(long rx) {
        this.rx = rx;
    }
}

 
 
  • 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
  • 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

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_app"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

app_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/app_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/app_tx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/app_rx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>
 
 
  • 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
  • 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

运行界面如下: 
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计思路: Android流量监控软件需要实时地监控网络流量的使用情况,因此需要在后台持续地监听网络流量的变化。为了保证软件的实时性和准确性,我们可以采用以下设计思路: 1. 获取网络流量信息:使用Android系统提供的TrafficStats,可以获取手机当前的网络流量信息,包括总的流量和应用程序的流量。 2. 后台监控:通过Service组件,可以在后台持续地监听网络流量的变化,实时更新流量数据。 3. 显示流量信息:通过使用Android的Notification和Widget组件,可以将实时的流量信息以通知和小部件的形式展现在用户的桌面上,方便用户随时了解当前的流量使用情况。 实现步骤: 1. 创建一个Service组件,用于后台监听网络流量的变化,并实时更新流量数据。 2. 在Service组件,使用TrafficStats获取当前的网络流量信息,并将其保存到SharedPreferences。 3. 使用Handler和Timer组件,定时从SharedPreferences读取流量信息,并更新通知和小部件流量数据。 4. 使用Android的Notification和Widget组件,创建通知和小部件,展示实时的流量信息。 5. 在设置界面,提供用户自定义流量使用情况的功能,例如设置流量警告值、流量限制值等。 6. 在应用程序,提供流量使用记录的功能,可以查看每个应用程序的流量使用情况。 注意事项: 1. 由于Android系统的限制,无法获取其他应用程序的网络流量信息,因此只能获取本应用程序的流量信息。 2. 在后台监听网络流量的变化,需要使用WakeLock保持设备唤醒状态,确保Service组件可以持续工作。 3. 在更新通知和小部件流量数据时,需要考虑线程安全的问题,避免多线程竞争导致数据不一致的情况。 4. 在使用Notification和Widget组件时,需要注意用户的隐私保护,避免泄露用户的个人信息。 以上是Android流量监控软件的设计和实现思路,具体的代码实现可以参考相关的开源项目和文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值