Connectivity Manager 상세 정보 - 네트워크 연결 상태 해결

2044 단어
    /** 
      *   ConnectivityManager 
      * ConnectivityManager               
      *    TelephonyManager      、         ;WifiManager    wifi     。 
      *        ,         
      * NetworkInfo     wifi mobile             ,   getState()     State       
      *          。 
      *  
      */  
     public void testConnectivityManager() {  
      ConnectivityManager connManager = (ConnectivityManager) this  
        .getSystemService(CONNECTIVITY_SERVICE);  
      //          NetWorkInfo    
      NetworkInfo networkInfo = connManager.getActiveNetworkInfo();  
      //                
      boolean available = networkInfo.isAvailable();  
      if(available){  
       Log.i("  ", "         ");  
      }  
      else{  
       Log.i("  ", "         ");  
      }  
      
      State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
      if(State.CONNECTED==state){  
       Log.i("  ", "GPRS     ");  
      }  
        
      state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
      if(State.CONNECTED==state){  
       Log.i("  ", "WIFI     ");  
      }  
        
      //              
      startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));  
      //      wifi        
      startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));  
        
     }  

동시에 gettype으로 판단할 수 있습니다
Context context = listActivity.getApplicationContext();//       
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);//         
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();//         
if(activeNetInfo.getType()==ConnectivityManager.TYPE_WIFI){
//  WIFI 
}else if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) {
//  3G 
}

좋은 웹페이지 즐겨찾기