네트워크 접속 여부 확인하기 (wifi, mobile data)

프로그래밍/Android (Java)|2015. 1. 27. 10:57
반응형

네트워크 접속 여부를 확인하기 위한 파일 및 소스


AndroidManifest.xml


<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 



SysdocuActivity.java


import android.widget.Toast;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;


public class SysdocuActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sysdocu);

checkNetworkStatus();

}


// Network Connection Check

public void checkNetworkStatus() {

final ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);

final android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

final android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


if(mobile.isConnected() || wifi.isConnected()) {

Toast.makeText(this, "Network connect success", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(this, "Network connect fail", Toast.LENGTH_SHORT).show();

}

}


반응형

댓글()