zoukankan      html  css  js  c++  java
  • 手机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"/>
    

     效果图

  • 相关阅读:
    vue-cli3.0配置开发环境,测试环境,线上环境
    jQuery使用CDN加速
    浏览器中JavaScript脚本代码的load、ready等方法的加载顺序
    使用 JavaScript 拦截和跟踪浏览器中的 HTTP 请求
    Node和NPM在Windows环境下绿色安装及配置
    Nodejs 中将html转换成pdf文件
    数学励志公式:每天进步一点点
    网页调用打印机打印时纸张A4的设置
    用JS或jQuery访问页面内的iframe,兼容IE/FF
    HTML to DOM
  • 原文地址:https://www.cnblogs.com/loaderman/p/6509069.html
Copyright © 2011-2022 走看看