获取文件下载速率

      首先,创建一个服务NetService来发送广播

 private long total_data= TrafficStats.getTotalRxBytes();
    private Handler mHandler;
    //几秒刷新一次
    private final int count=3;



      在NetService定义一个线程来定时获取网速

/**
     * 定义线程周期性获取网速
     */
    private Runnable mRunnable=new Runnable() {
        @Override
        public void run() {
            //定时器
            mHandler.postDelayed(mRunnable,count*1000);
            Message msg=mHandler.obtainMessage();
//            Log.e("TAG","Handler中的网速是:"+getNetSpeed());
            msg.what=1;
            msg.arg1=getNetSpeed();
            mHandler.sendMessage(msg);
        }
    };



其中,主要的就是getNetSpeed()方法

/**
     * 核心方法,得到当前网速
     * @return
     */
    private int getNetSpeed() {
       long traffic_data=TrafficStats.getTotalRxBytes()-total_data;
        total_data=TrafficStats.getTotalRxBytes();
        return (int) (traffic_data/count);
    }



在onCreate()中创建消息发送

  @Override
    public void onCreate() {
        super.onCreate();
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==1){
                    float real_data= (float)msg.arg1;
                    DecimalFormat decimalFormat=new DecimalFormat(".00");
//                    Log.e("TAG","Handler中的网速是:"+real_data);
                    if(msg.arg1>1024){
                        if(msg.arg1>1024*1024){
                            Intent intent=new Intent();
                            intent.setAction("cn.timeon.docdisk.docdisk");

//格式化正在下载的速率
//                            float round = (float) (Math.round(((float)(real_data / (1024 * 1024)) * 100) / 100));
                            String format = decimalFormat.format(real_data / (1024 * 1024));
                            intent.putExtra("speed", format + "MB/s");
                            sendBroadcast(intent);
                        }else {
                            Intent intent=new Intent();
                            intent.setAction("cn.timeon.docdisk.docdisk");
                            String format = decimalFormat.format(real_data / 1024);
                            intent.putExtra("speed", format + "KB/s");
                            sendBroadcast(intent);
                        }


                    }else {
                        Intent intent=new Intent();
                        intent.setAction("cn.timeon.docdisk.docdisk");
                        String format = decimalFormat.format(real_data);
                        intent.putExtra("speed", format + "B/s");
                        sendBroadcast(intent);
                    }
                }
            }
        };
    }



在onStart()方法中开启线程

 @Override
    public void onStart(Intent intent, int startId) {
        mHandler.postDelayed(mRunnable, 0);
    }


在onDestroy()中移除线程

 @Override
    public void onDestroy() {
        mHandler.removeCallbacks(mRunnable);
        super.onDestroy();
    }




在要使用的Activity中开启服务,注册广播接收者

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_list);
        
        openService();
        initData();
    }


    private void initData() {
        iv_back.setOnClickListener(this);
        TransferAdapter adapter=new TransferAdapter(getSupportFragmentManager());
        vp_transfer.setAdapter(adapter);
        slidingTab.setViewPager(vp_transfer);
    }
    private void openService() {
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("cn.timeon.docdisk.docdisk");
        broadcastReceiver=new BroadcastReceiver(){


            @Override
            public void onReceive(Context context, Intent intent) {
//                netSpeed=intent.getStringExtra("speed");
                DownloadAdapter.getNetSpeed(intent.getStringExtra("speed"));
                Log.e("TAG", "当前的网速是:" + intent.getStringExtra("speed"));
            }
        };
        registerReceiver(broadcastReceiver,intentFilter);
        startService(new Intent(TransferListActivity.this, NetService.class));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(TransferListActivity.this, NetService.class));
        unregisterReceiver(broadcastReceiver);
    }



在清单文件中加入要使用的服务

<service android:name=".service.NetService"/>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值