标签:
我的手机管家(12) 网络助手 逻辑代码
public class
提供网络流量统计的类。这些统计数据包括字节发送和接收网络发送和接收的数据包,
在所有的接口,在手机界面,并对每一个用户基础。
这些统计数据可能无法在所有平台上使用。如果该设备不支持统计数据,则将返回 UNSUPPORTED
。
接收和发送数据包
static long getMobileRxPackets()
自设备启动后的整个移动网络中接收的数据包的返回数。
static long getMobileTxPackets()
发送
接收和发送的字节数
static long getMobileRxBytes()
接收
static long getMobileTxBytes()
自启动设备启动后的移动网络中传输的字节数。
给定的uid的应用接收和发送的字节数
getUidRxBytes(int uid) 接收
getUidTxBytes(int uid) 发送
/** *获取使用流量信息 */ private final Runnable runnable = new Runnable(){ public void run() { final PackageManager pm = NetworkHelperActivity.this.getPackageManager(); ArrayList<ResolveInfo> trafficlists = new ArrayList<ResolveInfo>(); Intent intent = new Intent(); intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); final List<ResolveInfo> lists = pm.queryIntentActivities(intent, 0); for(ResolveInfo info : lists){ if(TrafficStats.getUidRxBytes(info.activityInfo.applicationInfo.uid) != -1 && TrafficStats.getUidTxBytes(info.activityInfo.applicationInfo.uid) != -1){ trafficlists.add(info); } } //初始化适配器,并绑定 adapter = new MyNetworkAdapter(NetworkHelperActivity.this,trafficlists); lv.setAdapter(adapter); } } ;
逻辑代码:
package com.chb.myphonesave.activity; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.TrafficStats; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Window; import android.widget.ListView; import android.widget.TextView; import com.chb.myphonesave.R; import com.chb.myphonesave.adapter.MyNetworkAdapter; /** * 网络助手 */ public class NetworkHelperActivity extends Activity { private ListView lv; private MyNetworkAdapter adapter; private Timer timer; private Handler handler = new Handler(){ public void handleMessage(Message msg) { super.handleMessage(msg); adapter.notifyDataSetChanged(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_network_helper); lv = (ListView) findViewById(R.id.nt_lv_content); TextView tvgprs = (TextView) findViewById(R.id.gprs); TextView tvwifi = (TextView)findViewById(R.id.wifi); tvgprs.setText(TrafficStats.getMobileRxPackets() + TrafficStats.getMobileTxPackets() + " 包"); tvwifi.setText(TrafficStats.getTotalRxPackets() + TrafficStats.getTotalTxPackets() + " 包"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); TimerTask task = new TimerTask(){ @Override public void run() { Message msg = new Message(); handler.sendMessage(msg); } }; } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); //如果在ui界面,运行线程,否则,添加到ui界面的时间队列中 runOnUiThread(runnable); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); timer.cancel(); } /** *获取使用流量信息 */ private final Runnable runnable = new Runnable(){ public void run() { final PackageManager pm = NetworkHelperActivity.this.getPackageManager(); ArrayList<ResolveInfo> trafficlists = new ArrayList<ResolveInfo>(); Intent intent = new Intent(); intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); final List<ResolveInfo> lists = pm.queryIntentActivities(intent, 0); for(ResolveInfo info : lists){ if(TrafficStats.getUidRxBytes(info.activityInfo.applicationInfo.uid) != -1 && TrafficStats.getUidTxBytes(info.activityInfo.applicationInfo.uid) != -1){ trafficlists.add(info); } } //初始化适配器,并绑定 adapter = new MyNetworkAdapter(NetworkHelperActivity.this,trafficlists); lv.setAdapter(adapter); } } ; }
标签:
原文地址:http://www.cnblogs.com/chengbao/p/5642357.html