【android学习】断线重连机制

【解决问题】

android端连接网络之后,当网络断开连接时,为了提高用户体验,android自动检测网络,当有网络时,用户无需进行多余操作,android端自动重新连接网络。

【解决方案】

1、设置网络断开连接标志:NET_BAD

1)每次发送数据,若发送数据不成功,检测网络状态,若断开则置网络状态为NET_BAD

2)若手机网络关闭,则置网络状态为NET_BAD

如果网络未连接,做网络断开处理:

1)通知主线程,弹出对话框表示网络断开连接,提醒用户。

2)重新连接网络,保存用户信息,若网络连接成功,则将用户信息重新发送给服务器。

2、检测设备网络是否开启;

调用:DeviceNetUtil.isConnected(mContext)方法,返回true表示网络开启,返回false则置网络状态为NET_BAD

或者服务器断开也是网络断开,置网络状态为NET_BAD



import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.support.v4.net.ConnectivityManagerCompat;

public class DeviceNetUtil {
	private DeviceNetUtil() {
	}

	/**
	 * 检查当前WIFI是否连接,两层意思——是否连接,连接是不是WIFI
	 * 
	 * @param context
	 * @return true表示当前网络处于连接状态,且是WIFI,否则返回false
	 */
	public static boolean isWifiConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info != null && info.isConnected() && ConnectivityManager.TYPE_WIFI == info.getType()) {
			return true;
		}
		return false;
	}

	/**
	 * 检查当前GPRS是否连接,两层意思——是否连接,连接是不是GPRS
	 * 
	 * @param context
	 * @return true表示当前网络处于连接状态,且是GPRS,否则返回false
	 */
	public static boolean isGprsConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info != null && info.isConnected() && ConnectivityManager.TYPE_MOBILE == info.getType()) {
			return true;
		}
		return false;
	}

	/**
	 * 检查当前是否连接
	 * 
	 * @param context
	 * @return true表示当前网络处于连接状态,否则返回false
	 */
	public static boolean isConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info != null && info.isConnected()) {
			return true;
		}
		return false;
	}

	/**
	 * 对大数据传输时,需要调用该方法做出判断,如果流量敏感,应该提示用户
	 * 
	 * @param context
	 * @return true表示流量敏感,false表示不敏感
	 */
	public static boolean isActiveNetworkMetered(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		return ConnectivityManagerCompat.isActiveNetworkMetered(cm);
	}

	public static Intent registerReceiver(Context context, ConnectivityChangeReceiver receiver) {
		return context.registerReceiver(receiver, ConnectivityChangeReceiver.FILTER);
	}

	public static void unregisterReceiver(Context context, ConnectivityChangeReceiver receiver) {
		context.unregisterReceiver(receiver);
	}

	public static abstract class ConnectivityChangeReceiver extends BroadcastReceiver {

		public static final IntentFilter FILTER = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

		@Override
		public final void onReceive(Context context, Intent intent) {

			try {
				ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
				NetworkInfo gprsInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

				// 判断是否是Connected事件
				boolean wifiConnected = false;
				boolean gprsConnected = false;
				if (wifiInfo != null && wifiInfo.isConnected()) {
					wifiConnected = true;
				}
				if (gprsInfo != null && gprsInfo.isConnected()) {
					gprsConnected = true;
				}
				if (wifiConnected || gprsConnected) {
					onConnected();
					return;
				}

				// 判断是否是Disconnected事件,注意:处于中间状态的事件不上报给应用!上报会影响体验
				boolean wifiDisconnected = false;
				boolean gprsDisconnected = false;
				if (wifiInfo == null || wifiInfo != null && wifiInfo.getState() == State.DISCONNECTED) {
					wifiDisconnected = true;
				}
				if (gprsInfo == null || gprsInfo != null && gprsInfo.getState() == State.DISCONNECTED) {
					gprsDisconnected = true;
				}
				if (wifiDisconnected && gprsDisconnected) {
					onDisconnected();
					return;
				}
			} catch (Exception e) {

				LogUtil.i("error", "DeviceNetUtil ConnectivityChangeReceiver->onReceive->" + e.getMessage());
			}
		}

		protected abstract void onDisconnected();

		protected abstract void onConnected();
	}
}

3、为了保持客户端和服务器同步在线,建立心跳更新机制。

即隔一会儿,互相发送数据,若长时间未接收到数据,判定为对方掉线或断开连接。



你可能感兴趣的:(移动开发)