๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
APP/ANDROID

[๋ถ€์ŠคํŠธ์ฝ”์Šค] ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์ƒํƒœ ํ™•์ธ

by mingzoo 2020. 3. 11.

์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์ƒํƒœ ํ™•์ธํ•˜๊ธฐ

์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์ƒํƒœ๋ฅผ ๊ณ„์† ๋Œ๋ฆฌ๋ฉด์„œ ํ™•์ธํ•˜์—ฌ ๋ฌด์„ ๋žœ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์—ˆ๋Š”์ง€, 3G/LTE๋กœ ์—ฐ๊ฒฐ๋œ ์ƒํƒœ์ธ์ง€, ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ธํ„ฐ๋„ท์ด ์—ฐ๊ฒฐ๋ผ์žˆ์„ ๋•Œ์™€ ์•„๋‹๋•Œ ์–ด๋–ป๊ฒŒ ํ• ๊ฒƒ์ธ์ง€๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋‹ค.

 

 

//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์—ฐ๊ฒฐ์ƒํƒœ ์ฒดํฌ"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="๋Œ€๊ธฐ์ค‘"
        android:textSize="50dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//MainActivity.java

package com.example.connectstatus;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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 = (TextView) findViewById(R.id.textView);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                int status = NetworkStatus.getConnectivityStatus(getApplicationContext());
                if(status == NetworkStatus.TYPE_MOBILE){
                    textView.setText("๋ชจ๋ฐ”์ผ๋กœ ์—ฐ๊ฒฐ๋จ.");
                } else if (status == NetworkStatus.TYPE_WIFI){
                    textView.setText("๋ฌด์„ ๋žœ์œผ๋กœ ์—ฐ๊ฒฐ๋จ");
                } else{
                    textView.setText("์—ฐ๊ฒฐ ์•ˆ๋จ");
                }
            }
        });
    }
}

//NetworkStatus.java
package com.example.connectstatus;

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์‚ฌ์šฉ
    public static int getConnectivityStatus(Context context){
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        //๊ถŒํ•œ ํ•„์š”->AndroidManifest.xmlํŒŒ์ผ์„ ์—ด์–ด์„œ ์ถ”๊ฐ€

        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;
    }
}

์—ฐ๊ฒฐ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ๋•Œ๋Š” ์‹œ์Šคํ…œ ์„œ๋น„์Šค ๊ฐ์ฒด ์ค‘์—์„œ ConnectivityManager ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

NetworkStatus ํด๋ž˜์Šค ์•ˆ์—์„œ 

    public static final int TYPE_WIFI = 1;
    public static final int TYPE_MOBILE = 2;
    public static final int TYPE_NOT_CONNECTED = 3;

๋กœ ์ •์˜๋ฅผ ํ•ด์ค˜์„œ ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์ƒํƒœ์ค‘ ๋ฌด์„ ๋žœ(WiFi)๊ณผ ๊ด‘๋Œ€์—ญ๋ง(3G/LTE), ์—ฐ๊ฒฐ์•ˆ๋จ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ด๋ฅผ ์ƒ์ˆ˜๋กœ ์ •์˜ํ•œ ๊ฒƒ์ด๋‹ค.

getConnectivityStatus๋ฉ”์†Œ๋“œ๊ฐ€ ์ด ์„ธ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

ConnectivityManager ๊ฐ์ฒด์˜ getActiveNetworkInfo ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด NetworkInfo ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  ๊ทธ ์•ˆ์— ์ƒํƒœ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค.

 

NetworkStatus.java ํŒŒ์ผ์—์„œ NetworkInfo networkInfo = manager.getActiceNetworkInfo(); ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ• ํ…๋ฐ ์ด๋Š” ACCESS_NETWORK_STATE ๊ถŒํ•œ์„ ํ•„์š”๋กœํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ์˜ค๋ฅ˜๋Š” manifest -> AndroidManifest.xml ํŒŒ์ผ์„ ์—ด์–ด์„œ 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

 

 

728x90