首页 > 代码库 > TrafficStats——流量统计类的范例,获取实时网速
TrafficStats——流量统计类的范例,获取实时网速
2.3开始android就提供来这个类的API,这样我们就可以方便的用他来实现统计手机流量来。这个类其实也很简单,我贴上他的几个方法,大家一看就知道怎么用了。
static long getMobileRxBytes() //获取通过Mobile连接收到的字节总数,不包含WiFi
static long getMobileRxPackets() //获取Mobile连接收到的数据包总数
static long getMobileTxBytes() //Mobile发送的总字节数
static long getMobileTxPackets() //Mobile发送的总数据包数
static long getTotalRxBytes() //获取总的接受字节数,包含Mobile和WiFi等
static long getTotalRxPackets() //总的接受数据包数,包含Mobile和WiFi等
static long getTotalTxBytes() //总的发送字节数,包含Mobile和WiFi等
static long getTotalTxPackets() //发送的总数据包数,包含Mobile和WiFi等
static long getUidRxBytes(int uid) //获取某个网络UID的接受字节数,某一个进程的总接收量
static long getUidTxBytes(int uid) //获取某个网络UID的发送字节数,某一个进程的总发送量
下面说我做实时网速的思路:
1.获得当前的总接受数据,getTotalRxPackets()
2.每隔几秒再获取一次总接收的数据
3.讲最新获取的数据减去之前获取的数据并且除以间隔的秒数,就得到了每秒平均的网速b/s,最后进行单位转换为kb、Mb等等
实现:
1.得到当前网速的方法
初始时给total_data一个值:
private long total_data = http://www.mamicode.com/TrafficStats.getTotalRxBytes();
之后每次获取的数值要减去total_data再除以间隔秒数,并且将total重新赋值为最新的。
/** * 核心方法,得到当前网速 * @return */ private int getNetSpeed() { long traffic_data = http://www.mamicode.com/TrafficStats.getTotalRxBytes() - total_data; total_data = TrafficStats.getTotalRxBytes(); return (int)traffic_data /count ; }
2.周期性的获取网速
/** * 定义线程周期性地获取网速 */ private Runnable mRunnable = new Runnable() { @Override public void run() { //定时器 mHandler.postDelayed(mRunnable, count * 1000); Message msg = mHandler.obtainMessage(); msg.what = 1; msg.arg1 = getNetSpeed(); mHandler.sendMessage(msg); } };
3.单位转换
@Override public void onCreate() { super.onCreate(); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { //float real_data = http://www.mamicode.com/(float)msg.arg1; if(msg.arg1 > 1024 ){ System.out.println(msg.arg1 / 1024 + "kb/s"); } else{ System.out.println(msg.arg1 + "b/s"); } } } }; }
4.启动handler和停止handler
PS:服务开始就执行runnable中的方法,并且runnable中设置handler循环执行。执行runnable中run()方法时,将网速包装到message中,交给handler进行单位转换并显示(这里在handler中进行单位转换是为了方便显示到ui中)
/** * 启动服务时就开始启动线程获取网速 */ @Override public void onStart(Intent intent, int startId) { mHandler.postDelayed(mRunnable, 0); }; /** * 在服务结束时删除消息队列 */ @Override public void onDestroy() { mHandler.removeCallbacks(mRunnable); super.onDestroy(); };
@Override public void onCreate() { super.onCreate(); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { //float real_data = http://www.mamicode.com/(float)msg.arg1; if(msg.arg1 > 1024 ){ System.out.println(msg.arg1 / 1024 + "kb/s"); } else{ System.out.println(msg.arg1 + "b/s"); } } } };
最后,全部代码
Service:
package com.kale.netspeed;import android.app.Service;import android.content.Intent;import android.net.TrafficStats;import android.os.Handler;import android.os.IBinder;import android.os.Message;/** * @author:Jack Tony * @tips :实时获取当前网速的service * @date :2014-9-24 */public class Net_Service extends Service { private long total_data =http://www.mamicode.com/ TrafficStats.getTotalRxBytes(); private Handler mHandler; //几秒刷新一次 private final int count = 5; /** * 定义线程周期性地获取网速 */ private Runnable mRunnable = new Runnable() { @Override public void run() { //定时器 mHandler.postDelayed(mRunnable, count * 1000); Message msg = mHandler.obtainMessage(); msg.what = 1; msg.arg1 = getNetSpeed(); mHandler.sendMessage(msg); } }; @Override public void onCreate() { super.onCreate(); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { //float real_data = http://www.mamicode.com/(float)msg.arg1; if(msg.arg1 > 1024 ){ System.out.println(msg.arg1 / 1024 + "kb/s"); } else{ System.out.println(msg.arg1 + "b/s"); } } } }; } /** * 核心方法,得到当前网速 * @return */ private int getNetSpeed() { long traffic_data = http://www.mamicode.com/TrafficStats.getTotalRxBytes() - total_data; total_data = TrafficStats.getTotalRxBytes(); return (int)traffic_data /count ; } /** * 启动服务时就开始启动线程获取网速 */ @Override public void onStart(Intent intent, int startId) { mHandler.postDelayed(mRunnable, 0); }; /** * 在服务结束时删除消息队列 */ @Override public void onDestroy() { mHandler.removeCallbacks(mRunnable); super.onDestroy(); }; @Override public IBinder onBind(Intent intent) { return null; }}
<service android:name="com.kale.netspeed.Net_Service"/>
Activity:
package com.kale.netspeed;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(MainActivity.this,Net_Service.class)); }}
参考自:http://gundumw100.iteye.com/blog/1294167
TrafficStats——流量统计类的范例,获取实时网速