출처 : http://www.mtalking.com/?p=403
안드로이드 device 에서 3G나 WIFI 연결 정보를 알아볼때 사용
우선 AndroidManifest.xml에 " android.permission.ACCESS_NETWORK_STATE "  permission이 필요하다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ConnectivityManager cManager;
NetworkInfo mobile;
NetworkInfo wifi;
  
cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
mobile = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
wifi = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  
if(mobile.isConnected()){
    //3G 연결되있음.
}
  
if(wifi.isCOnnected()){
    //wifi 연결되있음.
}
위 코드는 필요시 한번 정보를 가지고 오면 끝이다.
그러나 네트워크 상황이 변화될때마다 특정 작업을 수행해야  한다면 다음과 같이 해야한다.
우선 BroadcastReceiver를 상속받는 class 를 만든다
ex)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class TestReceiver extends BroadcastReceiver {
  
    String action;
  
    @Override
    public void onReceive(Context context, Intent intent) {
  
        action=intent.getAction();
  
        Log.d("TestReceiver","action : " + action);
  
        if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
  
            /*
  
            네트워크 변화가 생길때마다 이쪽으로 들어온다.
  
            */
       }
  
    }
  
}
그다음 AndroidManifest.xml에 등록을 해준다.
ex)
1
2
3
4
5
<receiver android:name =".TestReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

+ Recent posts