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"/>
    

     效果图

  • 相关阅读:
    Razor中@:和text
    Razor返回HTML字符串
    EPPlus使用
    ASP.NET MVC使用AllowAnonymous特性跳过授权验证
    下拉框获取json文件的数据
    SpringBoot 监听机制
    MybatisPlus Warpper实现复杂查询
    整合MybatisPlus心得
    MybatisPlus性能分析插件
    MybatisPlus物理删除、逻辑删除
  • 原文地址:https://www.cnblogs.com/loaderman/p/6509069.html
Copyright © 2011-2022 走看看