最近发现自己的项目中没有主动监测网络状态的功能,都是被动掉线!所以在网上找了些资料实现了下!这里写下来,方便以后自己翻看,也方便大家学习指导!
本DOME中设计到了3方面的知识(这些具体知识介绍博客园有很多,这里不多介绍)
1.service(服务)
2.Receiver(广播)
3.Timer(计时器线程)
接下来直接上代码了!
1.创建一个接口保存网络状态
1 public interface GetConnectState { 2 3 public void GetState(boolean isConnected); 4 5 }
2.建一个类继承service,实现广播定义和注册,计时器的开启
1 @Override 2 public void onCreate() { 3 // TODO Auto-generated method stub 4 super.onCreate(); 5 //注册广播 6 this.mReceiver = new ConnectReceiver(); 7 IntentFilter intentFilter = new IntentFilter(); 8 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 9 registerReceiver(mReceiver, intentFilter); 10 } 11 12 /** 13 * 广播,将服务接收到的数据传到界面 14 * @author WJD 15 * 16 */ 17 public class ConnectReceiver extends BroadcastReceiver { 18 19 @Override 20 public void onReceive(Context context, Intent intent) { 21 // TODO Auto-generated method stub 22 if (intent.getAction().equals( 23 ConnectivityManager.CONNECTIVITY_ACTION)) { 24 Timer timer = new Timer(); 25 timer.schedule(new QunXTask(), new Date()); 26 } 27 28 } 29 30 } 31 32 /** 33 * 定时器线程 34 * 35 * @author WJD 36 * 37 */ 38 class QunXTask extends TimerTask { 39 40 @Override 41 public void run() { 42 // TODO Auto-generated method stub 43 if (isNetConnect()) { 44 isConnected = true; 45 } else { 46 isConnected = false; 47 } 48 49 //设置连接状态 50 if (onGetConnectState != null) { 51 onGetConnectState.GetState(isConnected); 52 } 53 } 54 55 }
下面是具体获取网络状态的方法,先获取到所有网络(wifi,移动。。。)逐一判断是否连接(如果要显示网络信息也很简单自己可以试试,获取的时候信息都包括了)
1 /** 2 * 判断是否有网络 3 * @return 4 */ 5 private boolean isNetConnect() { 6 //初始化网络管理器 7 ConnectivityManager conMag = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 8 if (conMag == null) { 9 return false; 10 } else { 11 //获取所有的网络连接,包括wifi,和移动网 12 NetworkInfo[] infos = conMag.getAllNetworkInfo(); 13 if (infos != null) { 14 for (int i = 0; i < infos.length; i++) { 15 //判断是否处于连接状态 16 if (infos[i].getState() == NetworkInfo.State.CONNECTED) { 17 return true; 18 } 19 } 20 } 21 } 22 return false; 23 }
4.现在回到界面,首先要开启服务
1 Intent intent = new Intent(); 2 intent.setClass(this, NetWrorkServer.class); 3 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
其次要定义一个监听器
1 /** 2 * 网络状态监听事件 3 */ 4 private ServiceConnection serviceConnection = new ServiceConnection() { 5 6 @Override 7 public void onServiceDisconnected(ComponentName name) { 8 // TODO Auto-generated method stub 9 10 } 11 12 @Override 13 public void onServiceConnected(ComponentName name, IBinder service) { 14 // TODO Auto-generated method stub 15 mNetWrorkServer = ((NetWrorkServer.MyBinder) service).getServer(); 16 mNetWrorkServer.setOnGetConnectState(new GetConnectState() { 17 18 @Override 19 public void GetState(boolean isConnected) { 20 // TODO Auto-generated method stub 21 if (isConnected) { 22 mHandler.sendEmptyMessage(1); 23 24 } else { 25 mHandler.sendEmptyMessage(0); 26 27 } 28 } 29 30 }); 31 32 } 33 };
然后就是显示,当然是用到Handler来更新UI
1 class MyHandler extends Handler { 2 @Override 3 public void handleMessage(Message msg) { 4 // TODO Auto-generated method stub 5 super.handleMessage(msg); 6 switch (msg.what) { 7 case 0: 8 text_mob.setText("网络连接失败"); 9 break; 10 case 1: 11 text_mob.setText("网络连接成功"); 12 break; 13 14 default: 15 break; 16 } 17 } 18 }
5.最后不要忘了把监听器关闭
1 @Override 2 protected void onDestroy() { 3 // TODO Auto-generated method stub 4 super.onDestroy(); 5 if (mNetWrorkServer != null) { 6 unbindService(serviceConnection); 7 } 8 9 }
希望有大牛指导,提供更强劲的思路!!!