转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811
目前,市面上有很多管理手机流量的软件,可以让用户实时获取到自己手机中当前消耗了多少流量,手机中每个应用程序消耗了多少手机流量,那么这些功能Android中是如何实现的呢,这篇文章就是要向大家介绍一下,Android系统中流量管理的功能。那么,就让我们一起来实现这些实用的功能吧。
一、原理
按照惯例,我们还是先来谈谈原理级别的东西。
Android系统中封装了一套流量数据API,这些API可以很好的管理Android系统流量使用情况。我们可以基于这些Android API来实现管理手机流量的功能。这些API很好的封装在了android.net包下的TrafficStats中,主要的方法有
//2g/3g接收的流量
TrafficStats.getMobileRxBytes();
//2g/3g接收的包信息
TrafficStats.getMobileRxPackets();
//2g/3g上传的流量
TrafficStats.getMobileTxBytes();
//2g/3g上传的包信息
TrafficStats.getMobileTxPackets();
//手机总共接收的流量
TrafficStats.getTotalRxBytes();
//手机总共上传的流量
TrafficStats.getTotalTxBytes();
//得到某个应用程程序接收的流量
TrafficStats.getUidRxBytes(uid);
//得到某个应用程程序接收的流量
TrafficStats.getUidTxBytes(uid);
我们就基于这些方法实现一个Android手机流量管理的程序示例。
二、实现
1、创建实体类TrafficInfo
为了更好的提现java面向对象的思想,我将获取的手机应用程序的信息封装成一个javabean,这个javabean实体类封装了应用程序的图标,包名,名称,用户id等信息,
具体实现代码如下:
package cn.lyz.mobilesafe.domain;import android.graphics.drawable.Drawable;/*** 流量信息* @author liuyazhuang**/
public class TrafficInfo {//应用图标private Drawable icon;//app名称private String appname;//包名private String packname;//uidprivate int uid;public TrafficInfo() {super();// TODO Auto-generated constructor stub}public TrafficInfo(Drawable icon, String appname, String packname, int uid) {super();this.icon = icon;this.appname = appname;this.packname = packname;this.uid = uid;}public Drawable getIcon() {return icon;}public void setIcon(Drawable icon) {this.icon = icon;}public String getAppname() {return appname;}public void setAppname(String appname) {this.appname = appname;}public String getPackname() {return packname;}public void setPackname(String packname) {this.packname = packname;}public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}@Overridepublic String toString() {return "TrafficInfo [icon=" + icon + ", appname=" + appname+ ", packname=" + packname + ", uid=" + uid + "]";}
}
2、获取手机安装的应用程序的工具类TrafficManagerService
这个封装了主要封装了两个方法,一个是查询能够启动的应用程序方法getLauncherTrafficInfos,一个是获取拥有internet权限的应用列表的方法getInternetTrafficInfos,两个方法的返回值都是一个应用程序列表集合List<TrafficInfo>。
具体实现代码如下:
package cn.lyz.mobilesafe.engine;import java.util.ArrayList;
import java.util.List;import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import cn.lyz.mobilesafe.domain.TrafficInfo;/*** 获取手机安装的应用程序的的引擎工具类* @author liuyazhuang**/
public class TrafficManagerService {private PackageManager pm;public TrafficManagerService(Context context) {super();pm = context.getPackageManager();}/*** 查询能够启动的应用程序* @return*/public List<TrafficInfo> getLauncherTrafficInfos(){List<TrafficInfo> trafficInfos = new ArrayList<TrafficInfo>();//查询能够启动的应用程序 Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);//ResolveInfo 就类似于一个IntentFilterList<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);for(ResolveInfo info:resolveInfos){ApplicationInfo appInfo = info.activityInfo.applicationInfo;Drawable appicon = appInfo.loadIcon(pm);String appname = appInfo.loadLabel(pm).toString();String packageName = appInfo.packageName;int uid = appInfo.uid;trafficInfos.add(new TrafficInfo(appicon, appname, packageName, uid));}return trafficInfos;}/*** 获取拥有internet权限的应用列表* @return*/public List<TrafficInfo> getInternetTrafficInfos(){List<TrafficInfo> trafficInfos = new ArrayList<TrafficInfo>();//获取手机中安装的并且具有权限的应用List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_PERMISSIONS);for(PackageInfo info : installedPackages){//获取权限数组String[] permissions = info.requestedPermissions;if(permissions != null && permissions.length > 0){for(String permission : permissions){if(permission.equals(Manifest.permission.INTERNET)){ApplicationInfo applicationInfo = info.applicationInfo;Drawable icon = applicationInfo.loadIcon(pm);String appname = applicationInfo.loadLabel(pm).toString();String packagename = applicationInfo.packageName;int uid = applicationInfo.uid;TrafficInfo trafficInfo = new TrafficInfo(icon, appname, packagename, uid);trafficInfos.add(trafficInfo);}}}}return trafficInfos;}
}
3、创建文本格式化工具类TextFormat
当我们在Android系统中获取到流量信息时,有些流量的单位是byte,有些流量的单位是kb,为了统一显示这些流量信息单位,我在这里写了一个格式化流量单位的工具类。
具体实现代码如下:
package cn.lyz.mobilesafe.utils;import java.text.DecimalFormat;/*** 文本格式化工具类* @author liuyazhuang**/
public class TextFormat {/*** 格式化数据* @param data* @return*/public static String formatByte(long data){DecimalFormat format = new DecimalFormat("##.##");if(data < 1024){return data+"bytes";}else if(data < 1024 * 1024){return format.format(data/1024f) +"KB";}else if(data < 1024 * 1024 * 1024){return format.format(data/1024f/1024f) +"MB";}else if(data < 1024 * 1024 * 1024 * 1024){return format.format(data/1024f/1024f/1024f) +"GB";}else{return "超出统计范围";}}
}
4、程序入口TrafficManagerActivity
1)类成员字段
protected static final int SUCCESS_GET_TRAFFICINFO = 0;
protected static final int REFRESH_TRAFFIC = 1;
private TextView tv_traffic_manager_mobile;
private TextView tv_traffic_manager_wifi;
private ListView lv_traffic_manager_content;
private TrafficManagerService managerService;
private List<TrafficInfo> trafficInfos;
private List<TrafficInfo> realTrafficInfos;
private TrafficManagerAdapter mAdapter;
2)获取手机2g/3g总流量
/*** 获取手机2g/3g总流量* @return*/
private long getMobileTotal(){long mobile_rx = TrafficStats.getMobileRxBytes();long mobile_tx = TrafficStats.getMobileTxBytes();return (mobile_rx + mobile_tx) < 0 ? 0 : (mobile_rx + mobile_tx);
}
3)获取手机总流量
/*** 获取手机总流量* @return*/
private long total(){return TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes();
}
4)获取WIFI总流量
/*** 获取WIFI总流量* @return*/
private long getWifiTotal(){return total() - getMobileTotal();
}
5)自定义适配器TrafficManagerAdapter
这是个内部类,继承自BaseAdapter,主要实现了将获取到的数据显示到界面上的操作,我们这里用到了ListView的ViewHolder缓存技术。
具体实现代码如下:
static class ViewHolder{ImageView iv_appicon;TextView tv_appname;TextView tv_apptx;TextView tv_apprx;TextView tv_apptarffic;
}
private class TrafficManagerAdapter extends BaseAdapter{private LayoutInflater mLayoutInflater;public TrafficManagerAdapter(Context context){mLayoutInflater = LayoutInflater.from(context);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn realTrafficInfos.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn realTrafficInfos.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = null;ViewHolder holder = null;if(convertView != null){view = convertView;holder = (ViewHolder) view.getTag();}else{view = mLayoutInflater.inflate(R.layout.traffic_manager_item, null);holder = new ViewHolder();holder.iv_appicon = (ImageView) view.findViewById(R.id.iv_appicon);holder.tv_appname = (TextView) view.findViewById(R.id.tv_appname);holder.tv_apptx = (TextView) view.findViewById(R.id.tv_apptx);holder.tv_apprx = (TextView) view.findViewById(R.id.tv_apprx);holder.tv_apptarffic = (TextView) view.findViewById(R.id.tv_apptarffic);view.setTag(holder);}TrafficInfo info = realTrafficInfos.get(position);holder.iv_appicon.setImageDrawable(info.getIcon());String name = info.getAppname();if(name.length() > 8){name = name.substring(0, 7)+"...";}holder.tv_appname.setText(name);int uid = info.getUid();long tx = TrafficStats.getUidTxBytes(uid);if(tx < 0){tx = 0;}long rx = TrafficStats.getUidRxBytes(uid);if(rx < 0){rx = 0;}long total = tx + rx;holder.tv_apptarffic.setText(TextFormat.formatByte(total));holder.tv_apptx.setText("上传:"+TextFormat.formatByte(tx));holder.tv_apprx.setText("下载:"+TextFormat.formatByte(rx));return view;}}
6)Handler实现
我们通过一个Hanlder来实现主线程与子线程的交互操作,将子线程处理的结果数据,通过Handler与Message机制传递到主线程。
具体实现代码如下:
private Handler mHandler = new Handler(){public void handleMessage(android.os.Message msg) {switch (msg.what) {case SUCCESS_GET_TRAFFICINFO:mAdapter = new TrafficManagerAdapter(TrafficManagerActivity.this);lv_traffic_manager_content.setAdapter(mAdapter);timer = new Timer();timer.schedule(timerTask, 0,2000);break;case REFRESH_TRAFFIC:if(mAdapter != null){mAdapter.notifyDataSetChanged();}break;default:break;}};
};
7)实现定时操作
在这个示例程序中,我们需要一个定时器来实时获取手机流量数据,来实现动态实时效果的呈现,所以,我在这里用到了Timer和TimerTask两个工具类,来实现2秒钟向主线程发送一次message消息,由Handler来执行刷新界面显示的效果。
具体实现代码如下:
private Timer timer;
private TimerTask timerTask = new TimerTask() {@Overridepublic void run() {Message msg = new Message();msg.what = REFRESH_TRAFFIC;mHandler.sendMessage(msg);}
};
8)onCreate方法
这个方法主要实现的功能是初始化页面控件,设置控件事件,在子线程中实现获取手机流量信息等操作。
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.traffic_manager);tv_traffic_manager_mobile = (TextView) findViewById(R.id.tv_traffic_manager_mobile);tv_traffic_manager_wifi = (TextView) findViewById(R.id.tv_traffic_manager_wifi);lv_traffic_manager_content = (ListView) findViewById(R.id.lv_traffic_manager_content);tv_traffic_manager_mobile.setText(TextFormat.formatByte(getMobileTotal()));tv_traffic_manager_wifi.setText(TextFormat.formatByte(getWifiTotal()));managerService = new TrafficManagerService(this);new Thread(new Runnable() {@Overridepublic void run() {//trafficInfos = managerService.getLauncherTrafficInfos();realTrafficInfos = managerService.getInternetTrafficInfos();
// realTrafficInfos = new ArrayList<TrafficInfo>();
// for(TrafficInfo info : trafficInfos){
// if(TrafficStats.getUidRxBytes(info.getUid()) == -1 && TrafficStats.getUidTxBytes(info.getUid())== -1){
//
// }else{
// realTrafficInfos.add(info);
// }
// }Message msg = new Message();msg.what = SUCCESS_GET_TRAFFICINFO;msg.obj = trafficInfos;mHandler.sendMessage(msg);}}).start();
9)onDestroy方法
我们主要在这个回调方法中实现资源释放的操作。
具体实现代码如下:
@Override
protected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if(timer != null){timer.cancel();timer = null;}if(timerTask != null){timerTask.cancel();timerTask = null;}
}
10)完整源代码
package com.lyz.traffic.state.activity;import java.util.List;
import java.util.Timer;
import java.util.TimerTask;import android.app.Activity;
import android.content.Context;
import android.net.TrafficStats;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.lyz.mobilesafe.domain.TrafficInfo;
import cn.lyz.mobilesafe.engine.TrafficManagerService;
import cn.lyz.mobilesafe.utils.TextFormat;/*** 流量管理* @author liuyazhuang**/
public class TrafficManagerActivity extends Activity {protected static final int SUCCESS_GET_TRAFFICINFO = 0;protected static final int REFRESH_TRAFFIC = 1;private TextView tv_traffic_manager_mobile;private TextView tv_traffic_manager_wifi;private ListView lv_traffic_manager_content;private TrafficManagerService managerService;private List<TrafficInfo> trafficInfos;private List<TrafficInfo> realTrafficInfos;private TrafficManagerAdapter mAdapter;private Timer timer;private TimerTask timerTask = new TimerTask() {@Overridepublic void run() {Message msg = new Message();msg.what = REFRESH_TRAFFIC;mHandler.sendMessage(msg);}};private Handler mHandler = new Handler(){public void handleMessage(android.os.Message msg) {switch (msg.what) {case SUCCESS_GET_TRAFFICINFO:mAdapter = new TrafficManagerAdapter(TrafficManagerActivity.this);lv_traffic_manager_content.setAdapter(mAdapter);timer = new Timer();timer.schedule(timerTask, 0,2000);break;case REFRESH_TRAFFIC:if(mAdapter != null){mAdapter.notifyDataSetChanged();}break;default:break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.traffic_manager);tv_traffic_manager_mobile = (TextView) findViewById(R.id.tv_traffic_manager_mobile);tv_traffic_manager_wifi = (TextView) findViewById(R.id.tv_traffic_manager_wifi);lv_traffic_manager_content = (ListView) findViewById(R.id.lv_traffic_manager_content);tv_traffic_manager_mobile.setText(TextFormat.formatByte(getMobileTotal()));tv_traffic_manager_wifi.setText(TextFormat.formatByte(getWifiTotal()));managerService = new TrafficManagerService(this);new Thread(new Runnable() {@Overridepublic void run() {//trafficInfos = managerService.getLauncherTrafficInfos();realTrafficInfos = managerService.getInternetTrafficInfos();
// realTrafficInfos = new ArrayList<TrafficInfo>();
// for(TrafficInfo info : trafficInfos){
// if(TrafficStats.getUidRxBytes(info.getUid()) == -1 && TrafficStats.getUidTxBytes(info.getUid())== -1){
//
// }else{
// realTrafficInfos.add(info);
// }
// }Message msg = new Message();msg.what = SUCCESS_GET_TRAFFICINFO;msg.obj = trafficInfos;mHandler.sendMessage(msg);}}).start();// //2g/3g接收的流量
// TrafficStats.getMobileRxBytes();
// //2g/3g接收的包信息
// TrafficStats.getMobileRxPackets();
// //2g/3g上传的流量
// TrafficStats.getMobileTxBytes();
// //2g/3g上传的包信息
// TrafficStats.getMobileTxPackets();
// //手机总共接收的流量
// TrafficStats.getTotalRxBytes();
// //手机总共上传的流量
// TrafficStats.getTotalTxBytes();
// //得到某个应用程程序接收的流量
// TrafficStats.getUidRxBytes(uid);
// //得到某个应用程程序接收的流量
// TrafficStats.getUidTxBytes(uid);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}/*** 获取手机2g/3g总流量* @return*/private long getMobileTotal(){long mobile_rx = TrafficStats.getMobileRxBytes();long mobile_tx = TrafficStats.getMobileTxBytes();return mobile_rx + mobile_tx;}/*** 获取手机总流量* @return*/private long total(){return TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes();}/*** 获取WIFI总流量* @return*/private long getWifiTotal(){return total() - getMobileTotal();}static class ViewHolder{ImageView iv_appicon;TextView tv_appname;TextView tv_apptx;TextView tv_apprx;TextView tv_apptarffic;}private class TrafficManagerAdapter extends BaseAdapter{private LayoutInflater mLayoutInflater;public TrafficManagerAdapter(Context context){mLayoutInflater = LayoutInflater.from(context);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn realTrafficInfos.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn realTrafficInfos.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = null;ViewHolder holder = null;if(convertView != null){view = convertView;holder = (ViewHolder) view.getTag();}else{view = mLayoutInflater.inflate(R.layout.traffic_manager_item, null);holder = new ViewHolder();holder.iv_appicon = (ImageView) view.findViewById(R.id.iv_appicon);holder.tv_appname = (TextView) view.findViewById(R.id.tv_appname);holder.tv_apptx = (TextView) view.findViewById(R.id.tv_apptx);holder.tv_apprx = (TextView) view.findViewById(R.id.tv_apprx);holder.tv_apptarffic = (TextView) view.findViewById(R.id.tv_apptarffic);view.setTag(holder);}TrafficInfo info = realTrafficInfos.get(position);holder.iv_appicon.setImageDrawable(info.getIcon());String name = info.getAppname();if(name.length() > 8){name = name.substring(0, 7)+"...";}holder.tv_appname.setText(name);int uid = info.getUid();long tx = TrafficStats.getUidTxBytes(uid);if(tx < 0){tx = 0;}long rx = TrafficStats.getUidRxBytes(uid);if(rx < 0){rx = 0;}long total = tx + rx;holder.tv_apptarffic.setText(TextFormat.formatByte(total));holder.tv_apptx.setText("上传:"+TextFormat.formatByte(tx));holder.tv_apprx.setText("下载:"+TextFormat.formatByte(rx));return view;}}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if(timer != null){timer.cancel();timer = null;}if(timerTask != null){timerTask.cancel();timerTask = null;}}
}
5、布局文件
这里一个是主布局文件traffic_manager.xml和ListView的条目布局文件traffic_manager_item.xml
分别实现如下:
1)traffic_manager.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TableLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="2g/3g" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="wifi" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/tv_traffic_manager_mobile"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="200Mb" /><TextViewandroid:id="@+id/tv_traffic_manager_wifi"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="50MB" /></TableRow></TableLayout><SlidingDrawerandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:content="@+id/lv_traffic_manager_content"android:handle="@+id/handle"android:orientation="vertical" ><ImageViewandroid:id="@id/handle"android:layout_width="30dp"android:layout_height="30dp"android:src="@drawable/notification" /><ListViewandroid:id="@id/lv_traffic_manager_content"android:layout_width="fill_parent"android:layout_height="fill_parent" ></ListView></SlidingDrawer></LinearLayout>
2)traffic_manager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/iv_appicon"android:layout_width="35dip"android:layout_height="35dip"android:scaleType="fitXY"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/tv_appname"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/iv_appicon"android:text="我最摇摆"android:textColor="@android:color/white"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_apptarffic"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="20MB"android:textColor="@android:color/white"android:textSize="20sp" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/tv_appname"android:layout_toRightOf="@id/iv_appicon"android:orientation="horizontal" ><TextViewandroid:id="@+id/tv_apptx"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="80dp"android:text="上传:20MB"android:textSize="14sp" /><TextViewandroid:id="@+id/tv_apprx"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="下载:20MB"android:textSize="14sp" /></LinearLayout></RelativeLayout>
三、运行效果
四、温馨提示
大家可以到链接http://download.csdn.net/detail/l1028386804/9008451下载完整的Android流量管理程序示例源代码
本实例中,为了方面,我把一些文字直接写在了布局文件中和相关的类中,大家在真实的项目中要把这些文字写在string.xml文件中,在外部引用这些资源,切记,这是作为一个Android程序员最基本的开发常识和规范,我在这里只是为了方便直接写在了类和布局文件中。