您的位置:首页 > 理论基础 > 计算机网络

Android实时监听是否有网络,常驻广播——监测网络变化

2016-11-09 16:20 429 查看
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.chansh.csm.home.MainActivity;

/**
*
* @ClassName: MyBroadCast
* @Description: TODO(常驻广播——监测网络变化)
* @date 2015年6月5日 下午2:01:24
*
*/
public class MyBroadCast extends BroadcastReceiver {
public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(CONNECTIVITY_CHANGE_ACTION)) {
int type = UIConnect.isConnect_Type(context);
try {
if (type == 0) {
Intent in = new Intent(
MainActivity.BROADCAST_CHECK_CONNECTION);
context.sendBroadcast(in);
} else if (type == 1) {
if (UIConnect.c == null || !UIConnect.c.isShowing()) {
} else {
UIConnect.c.dismiss();
}
} else if (type == 2) {
Toast.makeText(context, "当前连接为手机网络,建议更换wifi环境",
Toast.LENGTH_LONG).show();
if (UIConnect.c != null || UIConnect.c.isShowing()) {
UIConnect.c.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: