通过一个activity总结
public class TrafficManagerActivity extends Activity {
private TrafficManagerService trafficManagerService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 2g3g 接收流量
TrafficStats.getMobileRxBytes();
//2g/3g 接收的包
TrafficStats.getMobileRxPackets();
//2g/3g 上传的流量
TrafficStats.getMobileTxBytes();
//2g/3g 上传的包
TrafficStats.getMobileTxPackets();
// 手机总共接收的流量
TrafficStats.getTotalRxBytes();
// 手机总共上传的流量
TrafficStats.getTotalTxBytes();
//得到某个应用程序接收的流量
TrafficStats.getUidRxBytes(uid);
TrafficStats.getUidTxBytes(uid);*/
trafficManagerService = new TrafficManagerService(this);
}
得到所有能启动流量的应用
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 就类似于一个IntentFilter
List<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;
}