android 通过广播监听网络连接状况

首先添加以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

new一个广播NetStateChangeBroadcast


package aviationboss.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.util.Log;
import android.widget.Toast;
import aviationboss.data.SystemData;

public class NetStateChangeBroadcast extends BroadcastReceiver {
private ConnectivityManager connectivity;
private boolean isConnect = false;

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
connectivity = (ConnectivityManager) arg0
.getSystemService(arg0.CONNECTIVITY_SERVICE);
State wifiState = connectivity.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).getState();
if (State.CONNECTED == wifiState) {
isConnect = true;
}

if(connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)!=null){
State mobileState = connectivity.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).getState();
if (State.CONNECTED == mobileState) {
isConnect = true;
}
}

Intent intent = new Intent();
intent.setAction("netConnect");

if (isConnect) {
if (!SystemData.NetState) {
Toast.makeText(arg0, "网络已连接", Toast.LENGTH_SHORT).show();
SystemData.NetState = true;
intent.putExtra("netActions", "网络已连接");
arg0.sendBroadcast(intent);
}
} else {
if (SystemData.NetState) {
Toast.makeText(arg0, "网络已断开", Toast.LENGTH_SHORT).show();
SystemData.NetState = false;
intent.putExtra("netActions", "网络已断开");
arg0.sendBroadcast(intent);
}
}
}
}

判断wifi的连接状态

connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()

判断移动网的连接状态

connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()

在配置文件中添加该广播,并设置监听的动作

<receiver android:name="aviationboss.broadcast.NetStateChangeBroadcast" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

时间: 2024-11-02 01:16:15

android 通过广播监听网络连接状况的相关文章

android 使用brocastReceiver监听网络连接状态

public class BroadcastService extends Service{ private ConnectivityManager connectivityManager;//网络连接管理器 private NetworkInfo networkInfo;//当前网络的信息 //点击查看 private PendingIntent messagePendingIntent = null; //通知栏消息 private Notification messageNotificat

Android开发之使用广播监听网络状态变化

我们经常需要判断网络状态的变化,如有无网络,所以需要监听网络状态的变化,比如网络断开,网络连接给予友好提示.如何监听网络状态的变化呢,最近工作中需要用到这个,于是就用广播机制来实现了网络状态的监听. 使用广播机制来监听网络状态主要涉及到以下几点: 1.需要自己编写个网络广播接收器,该接收器继承BroadcastReceiver类,用来接收系统发出的广播.比如我自己写了个NetStatusReceiver广播接收器.代码如下: package com.log.system; import andr

通过静态广播监听网络变化,在通过回调通知

package com.changim.patient.app.sys.receive; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import com.changim.patient.app.sys.Constants; import com.changim.patient.ap

android动态注册监听网络变化异常

在使用广播接收器监听网络变化的时候,在AndroidManifest.xml中加入<user-permission android:name="android.permission.ACCESS_NETWOEK_STATE"/>还是无法启动应用程序,显示如下异常 07-06 00:03:22.568 22393-22393/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.lvkui.helloworld3, PI

iOS开发之监听网络连接,改变,断开

做iOS开发时,我们需要监控/监听网络状况,苹果提供了Reachability.h, Reachability.m. 导入Reachability.h 我们可以在 MainViewController的viewDidLoad方法内部写上: [self checkReachability]; 之后,具体方法如下 #pragma mark #pragma mark Reachability Methods #pragma mark - (void)checkReachability { [[NSNo

Android利用广播监听按下HOME和电源键

package cc.testhome; import cc.testhome.HomeKeyObserver.OnHomeKeyListener; import cc.testhome.PowerKeyObserver.OnPowerKeyListener; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 利用广播监听Home键的按下和长按Home键 * 利用广播监听电源键的按下(关闭屏幕) * *

Android -- 使用广播监听Home键

//广播监听事件类 public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //do what you want System.exit(0); } } private HomeKeyEventBroadCastReceiver homeKeyEventReceiver; prote

Android使用BroadcastReceiver监听网络状态

在开发中,经常会遇到需要判断手机网络状态从而进行各种操作的功能,在这里简单介绍下这种功能的实现. 如题,通过BroadcastReceiver我们能在网络状态改变的时候监听到它. 那么,注册广播的两种方式: 1.静态注册:在AndroidManifest.xml中用标签声明注册,并在标签内用标签设置过滤器. 1 2 3 4 5 6 7 <!--继承BroadcastReceiver,重写onReceiver方法--> <receiver android:name="MyRece

Android实现广播监听HOME键操作

先写了个Service,在服务中通过广播来监听HOME键操作: public class HomeService extends Service{ private MonitoHomeReceiver mHomeBroadcastReceiver; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mHomeBro