android 应用在网络非常差的情况下的处理

app在无网络情况下非常好判断,

但是当网络很差,但是又有链接就不好办了。

使用的retrofit+rxJava,自定义了一个subscribe就可以很好的处理网络差的情况

public class MySubscrible<T> extends Subscriber<HttpResult<T>>  {

    private MySubscribleListener<HttpResult<T>> listener;
    private Context mContext;
//    private Intent intent;
    RefreshTask refreshTask;
    private Timer timer;

    public MySubscrible(Context mContext, MySubscribleListener<HttpResult<T>> listener) {
        this.listener = listener;
        this.mContext = mContext;
        refreshTask = new RefreshTask();
//        NetWorkManarge.getInstance().registerListtener(this);
    }


    @Override
    public void onStart() {
        super.onStart();
        if (timer == null) {
            timer = new Timer();
            timer.scheduleAtFixedRate(refreshTask, 0L, (long) 1000);
        }
//        intent = new Intent(mContext, NetWorkService.class);
//        mContext.startService(intent);
    }

    @Override
    public void onCompleted() {
        listener.onComplete();
        if(timer != null){
            timer.cancel();
            timer = null;
        }

        Log.i("NetworkSpeedService", "end network test");
    }

    @Override
    public void onError(Throwable e) {
        LogP.d(e.getMessage());
        if (e instanceof SocketTimeoutException) {
            Toast.makeText(mContext, "网络请求超时,请重试", Toast.LENGTH_SHORT).show();
        } else if (e instanceof ConnectException) {
            Toast.makeText(mContext, "网络中断,请检查您的网络状态", Toast.LENGTH_SHORT).show();
        } else if (e instanceof NullPointerException) {
            if (listener != null)
                Toast.makeText(mContext, "错误,请重试", Toast.LENGTH_SHORT).show();
//            Toast.makeText(mContext, "error:" + e.getMessage(), Toast.LENGTH_SHORT).show();
        } else if (e instanceof HttpException) {
            Toast.makeText(mContext, "网络中断,请检查您的网络状态", Toast.LENGTH_SHORT).show();
        } else if (e instanceof MyNetWorkSlowException) {
            listener.onNetWorkSlow();
        }else if (e instanceof UnknownHostException) {
            Toast.makeText(mContext, "网络错误,请检查您的网络后重试", Toast.LENGTH_SHORT).show();
        } else {
//            Toast.makeText(mContext, "error:" + e.getMessage(), Toast.LENGTH_SHORT).show();
            Toast.makeText(mContext, "系统错误,请重试", Toast.LENGTH_SHORT).show();
        }
        this.onCompleted();
    }

    @Override
    public void onNext(HttpResult<T> httpResult) {
        listener.onNext(httpResult);
    }

    public void onNetWorkSlow() {
        this.onError(new MyNetWorkSlowException("networkslow"));
        this.unsubscribe();
    }

    class RefreshTask extends TimerTask {
        boolean isFirst = true;
        private Handler handler = new Handler();
        private long rxtxTotal = 0;
        private boolean isNetBad = false;
        private int time;
        private double rxtxSpeed = 1.0f;
        private DecimalFormat showFloatFormat = new DecimalFormat("0.00");
        @Override
        public void run() {
            isNetBad = false;
            long tempSum = TrafficStats.getTotalRxBytes()
                    + TrafficStats.getTotalTxBytes();
            if (isFirst) {
                rxtxTotal = tempSum;
                isFirst = false;
            }
            long rxtxLast = tempSum - rxtxTotal;
            double tempSpeed = rxtxLast * 1000 / 1000;
            rxtxTotal = tempSum;
            if ((tempSpeed / 1024d) < 20 && (rxtxSpeed / 1024d) < 20) {
                time += 1;
            } else {
                time = 0;
            }
            rxtxSpeed = tempSpeed;
            Log.d("NetworkSpeedService", showFloatFormat.format(tempSpeed / 1024d) + "kb/s");
            if (time >= 16) {//连续16次检测网速都小于20kb/s  断定网速很差.
                isNetBad = true;
                Log.d("NetworkSpeedService", "网速差 " + isNetBad);
                time = 0; //重新检测
            }
            if (isNetBad) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
//                        receiverIntent.putExtra("is_slow_net_speed", isNetBad);
//                        sendBroadcast(receiverIntent);
//                        NetWorkService.this.stopSelf();
//                        NetWorkManarge.getInstance().notifyLoginData();
                        MySubscrible.this.onNetWorkSlow();
                    }
                });

            }
        }

    }
}
借鉴:https://www.jianshu.com/p/5678264e82d5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值