天天看點

手機APP流量的發送與擷取功能的實作

package com.loaderman.trafficdemo;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvMobileRxBytes;
    private TextView tvMobileTxBytes;
    private TextView tvTotalRxBytes;
    private TextView tvTotalTxBytes;
    private TextView tvAppRxBytes;
    private TextView tvAppTxBytes;
    private TextView tvAppWifiRxBytes;
    private TextView tvAppWifiTxBytes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMobileRxBytes = (TextView) findViewById(R.id.tv_mobileRxBytes);
        tvMobileTxBytes = (TextView) findViewById(R.id.tv_mobileTxBytes);
        tvTotalRxBytes = (TextView) findViewById(R.id.tv_totalRxBytes);
        tvTotalTxBytes = (TextView) findViewById(R.id.tv_totalTxBytes);
        tvAppRxBytes = (TextView) findViewById(R.id.tv_appRxBytes);
        tvAppTxBytes = (TextView) findViewById(R.id.tv_appTxBytes);
        tvAppWifiRxBytes = (TextView) findViewById(R.id.tv_appwifiRxBytes);
        tvAppWifiTxBytes = (TextView) findViewById(R.id.tv_appwifiTxBytes);
        method();
    }
    //上網讓其産生流量,Demo模拟使用
    public  void web(View view){
        startActivity(new Intent(this,WebViewActivity.class)) ;
    }
    public void method() {
        long totalRxBytes = TrafficStats.getTotalRxBytes(); //接收總流量, wifi + 移動
        long totalTxBytes = TrafficStats.getTotalTxBytes();//發送總流量, wifi+移動
        long mobileRxBytes = TrafficStats.getMobileRxBytes();//接收總流量, 移動
        long mobileTxBytes = TrafficStats.getMobileTxBytes();//發送總流量, 移動
        tvMobileRxBytes.setText("手機總接收流量:" + Formatter.formatFileSize(this, totalRxBytes));
        tvMobileTxBytes.setText("手機總發送流量:" + Formatter.formatFileSize(this, totalTxBytes));
        tvTotalRxBytes.setText("手機GPRS接收:" + Formatter.formatFileSize(this, mobileRxBytes));
        tvTotalTxBytes.setText("手機GPRS發送:" + Formatter.formatFileSize(this, mobileTxBytes));
        ConnectivityManager connectMgr = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        ApplicationInfo info = getApplicationInfo();
        NetworkInfo netinfo = connectMgr.getActiveNetworkInfo();
        if (netinfo != null || netinfo.isAvailable()) {
            if (netinfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                System.out.println(info.packageName);
                String name = info.packageName;
                if (name.equals("com.loaderman.trafficdemo") || name == "com.loaderman.trafficdemo") {
                    int uid = info.uid;
                    tvAppRxBytes.setText("本軟體GPRS接收:" + Formatter.formatFileSize(this, TrafficStats.getUidRxBytes(uid)));
                    tvAppTxBytes.setText("本軟體GPRS發送:" + Formatter.formatFileSize(this, TrafficStats.getUidTxBytes(uid)));
                }
            }
            if (netinfo.getType() == ConnectivityManager.TYPE_WIFI) {
                System.out.println(info.packageName);
                String name = info.packageName;
                if (name.equals("com.loaderman.trafficdemo") || name == "com.loaderman.trafficdemo") {
                    int uid = info.uid;
                    tvAppWifiRxBytes.setText("本軟體接收wifi:" + Formatter.formatFileSize(this, TrafficStats.getUidRxBytes(uid)));
                    tvAppWifiTxBytes.setText("本軟體發送wifi:" + Formatter.formatFileSize(this, TrafficStats.getUidTxBytes(uid)));
                }
            }
        }

    }
     //可見時再次重新整理
    @Override
    protected void onResume() {
        super.onResume();
        method();
    }
}
      
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>