网络(WiFi)断开不能监听系统发出的广播

划船不用桨--全靠浪 发布于 2012/03/19 18:00
阅读 2K+
收藏 0

public class ToolCheckNW{
    private static Activity context;
    private static ToolCheckNW instance ;
    //用于监听网络状态是否改变的广播
    private static networkRevice nw;
    //表示网络状态是否连接,false为断开
    private static boolean isLinkState = false;
  
    public  synchronized   static ToolCheckNW getInstance(Activity mcontext){
     context = mcontext;
       if(instance == null){
         instance = new ToolCheckNW();
         nw = new networkRevice();
      
         IntentFilter nwFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);  
         //增加要监听的状态

         //注册监听
         context.registerReceiver(nw, nwFilter);            
        }
        return instance;
    }
   
  
   
    /*网络状态发生改变的广播
     * ConnectivityManager.CONNECTIVITY_ACTION :用于监听网络状态是否改变。
     * ConnectivityManager.EXTRA_NO_CONNECTIVITY:监听网络是否连接
     */
   private static class networkRevice extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
 // TODO Auto-generated method stub
  String action = intent.getAction();
  Log.i("===============","---------xxxxxxxxxx---------" + action);
  Toast.makeText(context, "xxxxxxxxxxxxxxxxxx", 1).show();
  if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
   /*
    ConnectivityManager mConnMgr = (ConnectivityManager)
     context .getSystemService(Context.CONNECTIVITY_SERVICE);      
   NetworkInfo aActiveInfo =mConnMgr.getActiveNetworkInfo();
   Log.i("---------------","-----------------"+aActiveInfo.getState());
   if(aActiveInfo.getState() != null){
    isLinkState = true;
    Log.i("===============","---------dao---------" +isLinkState);
   }else{
    isLinkState = false;
   }*/
   
  } 
 }     
    }

}

加载中
返回顶部
顶部