时时监测网络变化

先简单说一下思路:网络变化时系统会发出广播。所以我们监听这个广播,利用接口回调通知activity做相应的操作就好了。。
步骤:
1、写个判断网络的工具类.
2、先写个类继承BroadcastReceiver。(不要忘记在清单文件中注册)
(谢谢ITzxl的提醒)需要在清单文件中添加权限
3、写个回调接口
4、BaseActivity实现这个接口
上代码:
/**
*
* @author cj 判断网络工具类
*
*/
public class NetUtil {
/**
* 没有连接网络
*/
private static final int NETWORK_NONE = -1;
/**
* 移动网络
*/
private static final int NETWORK_MOBILE = 0;
/**
* 无线网络
*/
private static final int NETWORK_WIFI = 1;

public static int getNetWorkState(Context context) {
    // 得到连接管理器对象
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {

        if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
            return NETWORK_WIFI;
        } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
            return NETWORK_MOBILE;
        }
    } else {
        return NETWORK_NONE;
    }
    return NETWORK_NONE;
}

}

/**
* 自定义检查手机网络状态是否切换的广播接受器
*
* @author cj
*
*/
public class NetBroadcastReceiver extends BroadcastReceiver {

public NetEvevt evevt = BaseActivity.evevt;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    // 如果相等的话就说明网络状态发生了变化
    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        int netWorkState = NetUtil.getNetWorkState(context);
        // 接口回调传过去状态的类型
        evevt.onNetChange(netWorkState);
    }
}

// 自定义接口
public interface NetEvevt {
    public void onNetChange(int netMobile);
}

}

记得在manifest中注册




abstract public class BaseActivity extends FragmentActivity implements NetEvevt {

public static NetEvevt evevt;
/**
 * 网络类型
 */
private int netMobile;

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    evevt = this;
    inspectNet();
}


/**
 * 初始化时判断有没有网络
 */

public boolean inspectNet() {
    this.netMobile = NetUtil.getNetWorkState(BaseActivity.this);
    return isNetConnect();

    // if (netMobile == 1) {
    // System.out.println("inspectNet:连接wifi");
    // } else if (netMobile == 0) {
    // System.out.println("inspectNet:连接移动数据");
    // } else if (netMobile == -1) {
    // System.out.println("inspectNet:当前没有网络");
    //
    // }
}

/**
 * 网络变化之后的类型
 */
@Override
public void onNetChange(int netMobile) {
    // TODO Auto-generated method stub
    this.netMobile = netMobile;
    isNetConnect();

}

/**
 * 判断有无网络 。
 * 
 * @return true 有网, false 没有网络.
 */
public boolean isNetConnect() {
    if (netMobile == 1) {
        return true;
    } else if (netMobile == 0) {
        return true;
    } else if (netMobile == -1) {
        return false;

    }
    return false;
}

}

public class MainActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    }

@Override
public void onNetChange(int netMobile) {
// TODO Auto-generated method stub
//在这个判断,根据需要做处理
}

}

在这需要说明一下,手机在开着wifi长时间不用,自动黑屏长时间,会关闭流量,所以在下拉刷新的时候,把监测状态的提升语给隐藏了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值