์ธํฐ๋ท ์ฐ๊ฒฐ์ํ ํ์ธํ๊ธฐ
์ธํฐ๋ท ์ฐ๊ฒฐ์ํ๋ฅผ ๊ณ์ ๋๋ฆฌ๋ฉด์ ํ์ธํ์ฌ ๋ฌด์ ๋์ผ๋ก ์ฐ๊ฒฐ๋์๋์ง, 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"/>๋ฅผ ์ถ๊ฐํ๋ฉด ๋๋ค.
'APP > ANDROID' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ถ์คํธ์ฝ์ค] ์๋๋ก์ด๋ ์นด๋ฉ๋ผ ์ฐ์ด์ ๋ํ๋ด๊ธฐ (0) | 2020.03.20 |
---|---|
[๋ถ์คํธ์ฝ์ค] ํฌํผ ์ฌ์ฉํ๊ธฐ (0) | 2020.03.11 |
[๋ถ์คํธ์ฝ์ค] SQL์ ์ด์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์ฉ ์์ (0) | 2020.03.11 |
[๋ถ์คํธ์ฝ์ค] ๋คํธ์ํน/HTTP (0) | 2020.03.05 |
[๋ถ์คํธ์ฝ์ค] ์ค๋ ๋(Thread) (0) | 2020.03.04 |