0. 출처
https://www.edwith.org/boostcourse-android/lecture/20491/
1. 실행 결과 확인
2. activity_main.xml
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 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context="com.tistory.qlyh8.practice.MainActivity"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="대기중" android:textSize="30sp"/> <Button android:id="@+id/button" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="네트워크 확인"/> </LinearLayout> | cs |
3. AndroidManifext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tistory.qlyh8.practice"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> | cs |
4. NetworkStatus.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 | package com.tistory.qlyh8.practice; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class NetworkStatus { public static final int TYPE_WIFI = 1; public static final int TYPE_MOBILE = 2; public static final int TYPE_NOT_CONNECTED = 3; public static int getConnectivityStatus(Context context){ // ConnectivityManager 시스템 서비스 객체를 사용하여 인터넷 연결 상태 확인 ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // 네트워크 정보 가져오기 NetworkInfo networkInfo = null; if (manager != null) {networkInfo = manager.getActiveNetworkInfo();} if (networkInfo != null) { int type = networkInfo.getType(); if (type == ConnectivityManager.TYPE_MOBILE) { // 3G, LTE 로 연결된 상태 return TYPE_MOBILE; } else if (type == ConnectivityManager.TYPE_WIFI) { // 와이파이로 연결된 상태 return TYPE_WIFI; } } return TYPE_NOT_CONNECTED; // 인터넷이 연결되지 않음 } } | cs |
5. MainActivity.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 | package com.tistory.qlyh8.practice; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int status = NetworkStatus.getConnectivityStatus(getApplicationContext()); if(status == NetworkStatus.TYPE_MOBILE) textView.setText("모바일로 연결됨\n"); else if(status == NetworkStatus.TYPE_WIFI) textView.setText("와이파이로 연결됨\n"); else textView.setText("연결되지 않음\n"); } }); } } | cs |
6. 참고
- 연결 상태 확인 및 모니터링
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring?hl=ko
'Android' 카테고리의 다른 글
카메라 미리 보여주기 (0) | 2018.08.23 |
---|---|
사진 찍어 보여주기 (0) | 2018.08.23 |
SQLiteOpenHelper 사용해보기 (0) | 2018.08.19 |
SQLite database 사용해보기 (0) | 2018.08.19 |
AsyncTask를 이용해 이미지 다운로드하기 (0) | 2018.08.04 |