[AndroidManifest.xml]
activity와 동등한 단계에 추가.
1
2
3
4
5
|
<receiver android:name = "appNetwork">
<intent-filter>
<action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"/>
</intent-filter>
</receiver>
|
[appNetwork.java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class appNetwork extends BroadcastReceiver {
private Activity activity;
public appNetwork() {
super();
}
public appNetwork(Activity activity) {
this.activity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action= intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
try {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo _wifi_network =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(_wifi_network != null) {
// wifi, 3g 둘 중 하나라도 있을 경우
if(_wifi_network != null && activeNetInfo != null){
}
// wifi, 3g 둘 다 없을 경우
else{
}
}
} catch (Exception e) {
Log.i("ULNetworkReceiver", e.getMessage());
}
}
}
}
|
[java 파일]
1
2
3
|
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
appNetwork receiver = new appNetwork(this);
registerReceiver(receiver, filter);
|
참고
댓글 없음:
댓글 쓰기