Android网络连接监听

分享到:

接收系统网络服务的广播接收者

 

public class NetStateReceiver extends BroadcastReceiver {
    private Handler handler;

    public NetStateReceiver(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void onReceive(Context context, Intent arg1) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!gprs.isConnected() && !wifi.isConnected()) {
            System.out.println("网络已断开");
            SysUtils.sendHandlerMsg(handler, SysConstant.MSG_NET_CONN_DISS);
        }

        if (gprs.isConnected() || wifi.isConnected()) {
            System.out.println("网络已连接");
            SysUtils.sendHandlerMsg(handler, SysConstant.MSG_NET_CONN_CONN);
        }
    }

}
昵    称:
验证码:

相关文档: