移动云

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 10136|回复: 0

获取android应用产生的流量

[复制链接]
发表于 2013-5-16 15:12:11 | 显示全部楼层 |阅读模式
package com.example.apptraffic;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.TrafficStats;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                try {
                        PackageManager pm = getPackageManager();
                        ApplicationInfo ai = pm.getApplicationInfo("org.ukpass.esp_nisk",
                                        PackageManager.GET_ACTIVITIES);
                        //根据uid获取发送的字节数
                        long send = TrafficStats.getUidTxBytes(ai.uid);

                        DecimalFormat format = new DecimalFormat("##0.00");
                        Double size = Double.parseDouble(String.valueOf(send));
                        if (size < 1024) {
                                Log.i("------send-----", size + " B");
                        } else if (1024 <= size && size < (1024 * 1024)) {
                                Log.i("------send-----", (format.format((size / 1024)) + " KB"));
                        } else if ((1024 * 1024) <= size) {
                                Log.i("------send-----",
                                                (format.format((size / 1024 / 1024)) + " M"));
                        }
                        //根据uid获取收到的字节数
                        long receive = TrafficStats.getUidRxBytes(ai.uid);

                        Double size2 = Double.parseDouble(String.valueOf(receive));
                        if (size2 < 1024) {
                                Log.i("------receive-----", size2 + " B");
                        } else if (1024 <= size2 && size2 < (1024 * 1024)) {
                                Log.i("------receive-----",
                                                (format.format((size2 / 1024)) + " KB"));
                        } else if ((1024 * 1024) <= size2) {
                                Log.i("------receive-----",
                                                (format.format((size2 / 1024 / 1024)) + " M"));
                        }
                } catch (NameNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
        }

}


http://developer.android.com/ref ... t/TrafficStats.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|管理员QQ:44994224|邮箱(t268studio@gmail.com)|Archiver|MCLOUDER

GMT+8, 2025-7-1 22:21 , Processed in 0.056931 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表