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