首页 > 代码库 > 手机安全卫士——流量管理

手机安全卫士——流量管理

首先先看一下界面

技术分享

布局文件都很简单,就不贴代码了!贴出主要的代码

  每一个不同的程序只能有一个UId,但是一个应用里面可以有多个PId,所以我们获取每个应用程序的uid

所以我们在软件管理里面用到的AppInfo 实体类和 AppInfos 逻辑处理 类里面加入uid字段,就可以满足我们的需求。

给AppInfo.java加入

 

private int uid;
    
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }

 

AppInfos.java中加入

 

int uid = installPackage.applicationInfo.uid;
appinfo.setUid(uid);

 

TrafficManagerActivity .java
public class TrafficManagerActivity extends Activity {
    
    private List<AppInfo> appInfos;
    private ListView lv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        initUI();
        initData();
    }

    private void initData() {
        // TODO Auto-generated method stub        
        new Thread(){
            

            public void run() {
                //得到所有应用程序的集合
                appInfos = AppInfos.getAppInfos(TrafficManagerActivity.this);
                System.out.println("得到所有应用程序的集合 list..");
                
//                Message msg = new Message();
//                hanlder.sendMessage(msg);
                
                runOnUiThread(new Runnable() {
                    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        MyAdapter adapter = new MyAdapter();
                        lv.setAdapter(adapter);
                    }
                });
            };
            
        }.start();
        
    }

    private void initUI() {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_traffic);
        lv = (ListView) findViewById(R.id.lv);            
    }

    
    
//    private Handler hanlder = new Handler(){
//        public void handleMessage(android.os.Message msg) {
//            MyAdapter adapter = new MyAdapter();
//            lv.setAdapter(adapter);
//            
//        };        
//    };
    
    
    
    public class MyAdapter extends BaseAdapter{

        
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return appInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return appInfos.get(position);
            
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int poition, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            
            ViewHolder holder;
            if(convertView!=null){
                
                holder = (ViewHolder) convertView.getTag();
            }else{
                convertView = LayoutInflater.from(TrafficManagerActivity.this).inflate( R.layout.item_traffic, null);
                holder = new ViewHolder();
                holder.icon = (ImageView)convertView.findViewById(R.id.iv_icon);
                holder.appname = (TextView) convertView.findViewById(R.id.tv_appname);
                holder.all = (TextView) convertView.findViewById(R.id.tv_all);
                holder.rx = (TextView) convertView.findViewById(R.id.tv_rx);
                holder.tx = (TextView) convertView.findViewById(R.id.tv_tx);
                
                convertView.setTag(holder);
            }    
            
            AppInfo info = appInfos.get(poition);
            holder.icon.setImageDrawable(info.getIcon());
            holder.appname.setText(info.getApkname());
            int uid = info.getUid();
            System.out.println("唯一标示: "+uid);
            long tx = TrafficStats.getUidTxBytes(uid);
            System.out.println("下载:"+tx);
            if(tx<0){
                tx = 0;
            }
            
            long rx = TrafficStats.getUidRxBytes(uid);
            if(rx<0){
                rx = 0;
            }
            long total = tx+rx;
            
            holder.rx.setText("下载:"+TextFormat.formatByte(rx));
            holder.tx.setText("上传:"+TextFormat.formatByte(tx));
            holder.all.setText("总流量:"+TextFormat.formatByte(total));
            
            return convertView;
        }
        
    }
    
    public class  ViewHolder{
        ImageView icon;
        TextView  appname;
        TextView tx;
        TextView rx;
        TextView all;
        
    }
    
    
    

}

TextFormat.java

import java.text.DecimalFormat;

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 "超出统计范围";  
        }  
    }  
}

 

手机安全卫士——流量管理