马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 青玄 于 2015-1-6 13:25 编辑
流量统计模块
核心代码:
<p>int uid=info.applicationInfo.uid;
long trafficRx=TrafficStats.getUidRxBytes(uid); //uid进程的总的接收量
long trafficTx=TrafficStats.long totalTrx=TrafficStats.getTotalRxBytes(); //手机上的总的流量包括手机流量与wifi流量
long totalTtx=TrafficStats.getTotalTxBytes();
long mobileRx=TrafficStats.getMobileRxBytes(); //手机上网的流</p><p> </p><p>量
long mobileTx=TrafficStats.getMobileTxBytes();getUidTxBytes(uid); //uid进程的总的发送量</p><p>*********************************************************************************</p><p> </p><p> </p><p>全部的代码:</p><p> </p><p>package cn.cbd.mobilesafe.activity.engine;</p><p>import java.util.ArrayList;
import java.util.List;</p><p>import cn.cbd.mobilesafe.activity.bean.ProcessTraffic;</p><p>import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.net.TrafficStats;
import android.util.Log;</p><p>public class TrafficProvider {
public static List<ProcessTraffic> getProcessTraffics(Context context)
{
List<ProcessTraffic> processTraffics=new ArrayList<ProcessTraffic>();
PackageManager packageManager=context.getPackageManager();
List<PackageInfo> packageInfos=packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
for(PackageInfo info :packageInfos)
{
String[] permissionInfos=info.requestedPermissions;
if(permissionInfos!=null && permissionInfos.length>0)
{
Log.i("TrafficProvider", info.packageName+" "+permissionInfos.length);
for(String pInfo : permissionInfos)
{
if("android.permission.INTERNET".equals(pInfo))
{
ProcessTraffic processTraffic=new ProcessTraffic();
processTraffic.setIcon(info.applicationInfo.loadIcon(packageManager));
processTraffic.setName(info.applicationInfo.loadLabel(packageManager).toString());
processTraffic.setPackageName(info.packageName);
int uid=info.applicationInfo.uid;
long trafficRx=TrafficStats.getUidRxBytes(uid);
long trafficTx=TrafficStats.getUidTxBytes(uid);
processTraffic.setTrafficRx(trafficRx);
processTraffic.setTrafficTx(trafficTx);
processTraffics.add(processTraffic);
break;
}
}
}
}
return processTraffics;
}</p><p>}
******************************************************************************</p><p> </p><p>主程序模块的代码:</p><p> </p><p>package cn.cbd.mobilesafe.activity;</p><p>import java.util.List;</p><p>import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.cbd.mobilesafe.activity.bean.ProcessTraffic;
import cn.cbd.mobilesafe.activity.engine.TrafficProvider;</p><p>public class TrafficManagerActivity extends Activity {
private TextView tv_2g_3g_traffic;
private TextView tv_wifi_traffic;
private ListView content;
private ListView lv_traffic_manager;
private LinearLayout ll_traffic_loading;
/**
* ProcessTraffic:
* private Drawable icon;
private String name;
private String packageName;
private long trafficRx;
private long trafficTx;
*/
private List<ProcessTraffic> processTraffics;
private MyTrafficAdapter myTrafficAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_traffic_manager);
tv_2g_3g_traffic=(TextView) findViewById(R.id.tv_2g_3g_traffic);
tv_wifi_traffic=(TextView) findViewById(R.id.tv_wifi_traffic);
content=(ListView) findViewById(R.id.content);
lv_traffic_manager=(ListView) findViewById(R.id.lv_traffic_manager);
ll_traffic_loading=(LinearLayout) findViewById(R.id.ll_traffic_loading);
long totalTrx=TrafficStats.getTotalRxBytes(); //手机上的总的流量包括手机流量与wifi流量
long totalTtx=TrafficStats.getTotalTxBytes();
long mobileRx=TrafficStats.getMobileRxBytes(); //手机上网的流量
long mobileTx=TrafficStats.getMobileTxBytes();
tv_2g_3g_traffic.setText("2G/3G总流量:"+Formatter.formatFileSize(this, mobileRx+mobileTx));
tv_wifi_traffic.setText("wifi总流量:"+Formatter.formatFileSize(this, totalTrx+totalTtx-mobileRx-mobileTx));
Log.i("bbb","123");
showData();
}
public void showData()
{
new AsyncTask<Void, Void, Void>(){</p><p>
@Override
protected void onPreExecute() {
ll_traffic_loading.setVisibility(View.VISIBLE);
super.onPreExecute();
}</p><p> </p><p> @Override
protected Void doInBackground(Void... params) {
processTraffics=TrafficProvider.getProcessTraffics(getApplicationContext());
Log.i("bbb",processTraffics.size()+"");
return null;
}
@Override
protected void onPostExecute(Void result) {
ll_traffic_loading.setVisibility(View.INVISIBLE);
myTrafficAdapter=new MyTrafficAdapter();
lv_traffic_manager.setAdapter(myTrafficAdapter);
super.onPostExecute(result);
}</p><p>
}.execute();
}
private class MyTrafficAdapter extends BaseAdapter
{</p><p> @Override
public int getCount() {
return processTraffics.size();
}</p><p> @Override
public Object getItem(int position) {
return processTraffics.get(position);
}</p><p> @Override
public long getItemId(int position) {
return position;
}</p><p> @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if(convertView!=null && convertView instanceof RelativeLayout)
{
view=convertView;
holder=(ViewHolder) view.getTag();
}
else
{
Log.i("aaa", processTraffics.size()+"");
view =View.inflate(getApplicationContext(), R.layout.layout_traffic_item, null);
holder=new ViewHolder();
holder.iv_icon=(ImageView) view.findViewById(R.id.iv_traffic_icon);
holder.tv_name=(TextView) view.findViewById(R.id.tv_traffic_name);
holder.tv_tx=(TextView) view.findViewById(R.id.tv_traffic_tx);
holder.tv_rx=(TextView) view.findViewById(R.id.tv_traffic_rx);
holder.tv_total=(TextView) view.findViewById(R.id.tv_traffic_total);
view.setTag(holder);
}
holder.iv_icon.setImageDrawable(processTraffics.get(position).getIcon());
holder.tv_name.setText(processTraffics.get(position).getName());
long tx=processTraffics.get(position).getTrafficTx();
long rx=processTraffics.get(position).getTrafficRx();
long total=tx+rx;
if(tx<0)
{
tx=0;
}
if(rx<0)
{
rx=0;
}
if(total<0);
{
total=0;
}
holder.tv_tx.setText("上传:"+Formatter.formatFileSize(getApplicationContext(), tx));
holder.tv_rx.setText("下载:"+Formatter.formatFileSize(getApplicationContext(), rx));
holder.tv_total.setText("总计:"+Formatter.formatFileSize(getApplicationContext(), total));
return view;
}
}</p><p> static class ViewHolder
{
private ImageView iv_icon;
private TextView tv_name;
private TextView tv_tx;
private TextView tv_rx;
private TextView tv_total;
}
</p><p>}
</font></font></font></b></p>
|