[안드로이드]네트워크 체크 - ConnectivityManager
현재 실 사용중인 네트워크 체크 소스입니다.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.intro);
Log.v("notice", "notice");
if (!KBONetworkInfo.IsWifiAvailable(this) && !KBONetworkInfo.Is3GAvailable(this))
{
//Toast.makeText(this, "네크워크에 연결할 수 없습니다.", Toast.LENGTH_LONG).show();
msgShow("알림","본 서비스는 네트워크를 이용하는 서비스로 현재 네트워크에 연결되어 있지 않습니다.","닫기");
return;
}
myHandler.postDelayed(myRunnable, 3000); // 딜레이 지정
}
public static class KBONetworkInfo {
public static boolean IsWifiAvailable(Context context)
{
ConnectivityManager m_NetConnectMgr= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean bConnect = false;
try
{
if( m_NetConnectMgr == null ) return false;
NetworkInfo info = m_NetConnectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
bConnect = (info.isAvailable() && info.isConnected());
}
catch(Exception e)
{
return false;
}
return bConnect;
}
public static boolean Is3GAvailable(Context context)
{
ConnectivityManager m_NetConnectMgr= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean bConnect = false;
try
{
if( m_NetConnectMgr == null ) return false;
NetworkInfo info = m_NetConnectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
bConnect = (info.isAvailable() && info.isConnected());
}
catch(Exception e)
{
return false;
}
return bConnect;
}
}
public void msgShow(String title,String msg,String closeMsg){
AlertDialog alert = new AlertDialog.Builder( this ).create();
alert.setTitle(title);
alert.setMessage( msg );
alert.setButton( closeMsg, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
finish();
}
});
alert.show();
}
public boolean onKeyDown(int keyCode, KeyEvent e) {
Log.v(null, "" + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return false;
}
public boolean onKeyDown1(int keyCode, KeyEvent e) {
Log.v(null, "" + keyCode);
if (keyCode == KeyEvent.KEYCODE_HOME) {
myHandler.removeCallbacks(myRunnable);
}
return false;
}