기기가 작동중인 네트워크에 연결되어 있는지 확인하는 데이 클래스를 사용합니다. 이 클래스는 가능한 모든 인터넷 제공 업체에서 작동합니다.
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
// 연결 검출기
ConnectionDetector의 CD = 새로운 ConnectionDetector (getApplicationContext());
그리고 isConnectingToInternet() 메소드를 인터넷 연결을 확인할 ConnectionDetector 클래스의 객체로 호출하십시오. 인터넷이 작동 중이면 false를 반환하고 그렇지 않으면 false를 반환합니다.
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(
MainActivity.this,
"Alert!!",
"Internet Connection is not on.Please check your network.",
false);
// stop executing code by return
return;
}
@OlafDietsche 죄송합니다. – turtleboy